JavaScript Strings
A string is a sequence of characters — text. Names, messages, HTML snippets, JSON and URLs are all strings. JavaScript strings can be written with single quotes, double quotes or backticks.
const single = 'Hello';
const double = "World";
const backtick = `Hi ${single}!`; // backticks allow embedded expressions
console.log(single, double, backtick);Hello World Hi Hello!
Single and double quotes behave identically — pick one style and stay consistent. Backticks create template literals, which get their own chapter.
Quotes inside strings
You can use one kind of quote inside a string delimited by another. Otherwise, escape the quote with a backslash:
console.log("It's easy");
console.log('She said "hi"');
console.log('It\'s escaped');
console.log("Line one\nLine two");
console.log("Tab\tseparated");
console.log("Backslash: \\");It's easy
She said "hi"
It's escaped
Line one
Line two
Tab separated
Backslash: \
| Escape | Meaning |
|---|---|
\' \" |
quote character |
\\ |
backslash |
\n |
new line |
\t |
tab |
é |
Unicode character (é) |
Length and characters
length gives the number of characters (UTF-16 code units). Each character has a position (index) starting at 0:
const word = "JavaScript";
console.log(word.length);
console.log(word[0]);
console.log(word[4]);
console.log(word.at(-1)); // last character
console.log(word[word.length - 1]); // the classic way
console.log(word[50]); // out of range10
J
S
t
t
undefined
Joining strings
+ concatenates strings, and += appends to a string variable:
const first = "Ada";
const last = "Lovelace";
let full = first + " " + last;
full += " (1815–1852)";
console.log(full);
console.log("Score: " + 10 + 5); // left to right: text first
console.log(10 + 5 + " points"); // numbers firstAda Lovelace (1815–1852)
Score: 105
15 points
Strings are immutable
You can't change a character inside an existing string — assigning to an index is silently ignored (or throws in strict mode). String methods always return a new string:
const greeting = "hello";
try {
greeting[0] = "J";
} catch (err) {
console.log(err.message);
}
const shout = greeting.toUpperCase();
console.log(greeting, shout);Cannot assign to read only property '0' of string 'hello'
hello HELLO
Comparing strings
=== compares strings character by character. < and > compare by Unicode code point — capital letters come before lowercase ones. For human-friendly alphabetical order use localeCompare:
console.log("apple" === "apple");
console.log("apple" < "banana");
console.log("Zebra" < "apple"); // "Z" (90) < "a" (97)
console.log("a".localeCompare("b")); // negative: a comes firsttrue
true
true
-1
String objects
new String("text") creates a String object rather than a primitive. Don't use it: it's slower and === comparisons break.
const a = "text";
const b = new String("text");
typeof b; // "object"
a === b; // falseTip: Primitive strings can still call methods like
toUpperCase()— JavaScript temporarily wraps them in an object behind the scenes.
Test yourself
What is "code".length?
- 3
- 4
- 5
- undefined
What does "cat"[1] return?
- "c"
- "a"
- "t"
- undefined
Which escape sequence produces a new line?
\t\n\r\b