Strings
Text in JavaScript — template literals, immutability, the most-used string methods, and Unicode.
Working with text in JavaScript
A string is just text — any characters between quotes. Names, messages, emails, URLs, addresses — any text your program needs to store or display is a string.
Three ways to write a string — and which to use
let a = 'single quotes'; // traditional
let b = "double quotes"; // traditional
let c = `backticks`; // modern — template literalsSingle and double quotes are identical — use whichever you prefer (pick one and be consistent). Backticks are different and more powerful — they're called template literals and you should reach for them whenever you need to combine text with variables.
Template literals — the JS version of Python f-strings
Old way (string concatenation — avoid this):
let name = "Alice";
let score = 95;
// ✗ Old way — clunky, hard to read:
console.log("Well done, " + name + "! Your score is " + score + " out of 100.");Modern way with template literals (backticks + ${}):
// ✓ Modern way — clean and readable:
console.log(`Well done, ${name}! Your score is ${score} out of 100.`);
// You can put ANY expression inside ${}:
let price = 9.99;
let qty = 3;
console.log(`Total: $${(price * qty).toFixed(2)}`); // Total: $29.97
// Multi-line strings — no
needed:
let message = `Dear ${name},
Your order has been confirmed.
Total: $${(price * qty).toFixed(2)}`;
console.log(message);Strings are immutable
Once a string is created, it can't be changed. String methods always return a new string — they never modify the original:
let word = "hello";
let upper = word.toUpperCase(); // returns a new string
console.log(word); // "hello" — unchanged!
console.log(upper); // "HELLO" — new stringThe most-used string methods
let text = " Hello, World! ";
// Clean up
text.trim() // "Hello, World!" — remove spaces from both ends
text.trimStart() // "Hello, World! " — left only
text.trimEnd() // " Hello, World!" — right only
// Case
text.trim().toLowerCase() // "hello, world!"
text.trim().toUpperCase() // "HELLO, WORLD!"
// Find things
text.trim().includes("World") // true — does it contain this?
text.trim().startsWith("Hello") // true
text.trim().endsWith("!") // true
text.trim().indexOf("World") // 7 — position (0-based), or -1 if not found
// Extract part of a string
let s = "JavaScript";
s.slice(0, 4) // "Java" — characters from index 0 up to (not including) 4
s.slice(4) // "Script" — from index 4 to the end
s.slice(-6) // "Script" — last 6 characters
// Replace
"Hello World".replace("World", "Codex") // "Hello Codex" — first match only
"aabbaa".replaceAll("a", "x") // "xxbbxx" — all matches
// Split and join (very useful)
"a,b,c".split(",") // ["a", "b", "c"] — string to array
["a","b","c"].join("-") // "a-b-c" — array to string
// Padding (useful for formatting)
"5".padStart(3, "0") // "005" — pad to length 3 with zeros
"hi".padEnd(5, "!") // "hi!!!" — pad rightString length and characters
let s = "Hello";
console.log(s.length); // 5
console.log(s[0]); // "H" — first character (0-based index)
console.log(s[4]); // "o" — last character
console.log(s.at(-1)); // "o" — last character (modern way)
console.log(s.at(-2)); // "l" — second-to-last"It's a great day" or 'Say "hello"' or
'It's escaped'.
Official Sources
- MDN Web Docs — String
- MDN Web Docs — Template literals
- ECMAScript 2024 — The String Type (§6.1.4)
- MDN Web Docs — String.prototype.at()
The Codex links only to official documentation and the ECMAScript specification. No third-party tutorials.