A string is text. You write it between quotes.
const single = 'Hello';
const double = "Hello";
const backtick = `Hello`;
console.log(single === double); // → true
console.log(double === backtick); // → true
Single and double quotes work the same. Use one style consistently. Backticks create a template literal, which has extra powers: see the next lesson, Template Literals.
Quotes inside strings
If the text contains the same quote you used, either switch the outer quote or escape the inner one with a backslash.
console.log("She said \"hi\""); // → She said "hi"
console.log('It\'s fine'); // → It's fine
console.log("It's fine"); // → It's fine (no escape needed)
Common escapes: \n (new line), \t (tab) and \\ (a backslash).
console.log("Line one\nLine two");
Length
.length gives the number of characters (strictly, UTF-16 code units).
const word = "JavaScript";
console.log(word.length); // → 10
console.log("".length); // → 0
Accessing characters
Positions start at 0.
const word = "Cat";
console.log(word[0]); // → C
console.log(word[2]); // → t
console.log(word.at(-1)); // → t (count from the end)
console.log(word[10]); // → undefined
.at(-1) is a modern way to read from the end. The older way is word[word.length - 1].
Strings are immutable
You cannot change a character in place. Methods return a new string.
const word = "cat";
console.log(word.toUpperCase()); // → CAT
console.log(word); // → cat (unchanged)
A note about emoji
"😀".length is 2. For most text this does not matter, but keep it in mind if you count characters for display.