Creating Strings
const single = "Hello";
const double = "World";
const template = `Hello, ${single}! Today is ${new Date().toDateString()}.`;
// Escape characters
const quote = "She said "hello"";
const newline = "Line 1
Line 2";
const tab = "Col1 Col2";Common String Methods
const str = "Hello, World!";
// Length
str.length // 13
// Case
str.toUpperCase() // "HELLO, WORLD!"
str.toLowerCase() // "hello, world!"
// Search
str.includes("World") // true
str.startsWith("Hello") // true
str.endsWith("!") // true
str.indexOf("o") // 4 (first occurrence)
str.lastIndexOf("o") // 8
// Extract
str.slice(7, 12) // "World"
str.slice(-6) // "World!" (from end)
str.substring(7, 12) // "World"
// Replace
str.replace("World", "JS") // "Hello, JS!"
str.replaceAll("l", "L") // "HeLLo, WorLd!"
"a-b-c".replace(/-/g, "_") // "a_b_c" (regex)
// Split / Join
"a,b,c".split(",") // ["a","b","c"]
["a","b"].join(" + ") // "a + b"
// Trim whitespace
" hello ".trim() // "hello"
" hello ".trimStart() // "hello "
" hello ".trimEnd() // " hello"
// Pad
"5".padStart(3, "0") // "005"
"hi".padEnd(5, ".") // "hi..."
// Repeat
"ha".repeat(3) // "hahaha"
// Check if number-like
Number("42") // 42
Number("abc") // NaN
parseInt("42px") // 42Template Literals
const name = "Alice";
const score = 95;
// Multi-line without
const html = `
<div class="card">
<h2>${name}</h2>
<p>Score: ${score}</p>
<p>Grade: ${score >= 90 ? "A" : "B"}</p>
</div>
`;Always use template literals for strings that contain variables or span multiple lines — they are far more readable than string concatenation with
+.