Creating Strings
const s = "hello";
const t = 'world';
const u = `Hello, ${name}!`; // Template literal
const m = `line 1
line 2`; // Multiline
String(123); // "123"
(42).toString(16); // "2a" (hex)
Searching
| Method | Returns | Description |
|---|
includes(str, pos?) | boolean | Contains substring |
startsWith(str, pos?) | boolean | Starts with substring |
endsWith(str, len?) | boolean | Ends with substring |
indexOf(str, pos?) | Index or -1 | First occurrence |
lastIndexOf(str, pos?) | Index or -1 | Last occurrence |
search(regex) | Index or -1 | Search with regex |
match(regex) | Array or null | Match against regex |
matchAll(regex) | Iterator | All matches (needs g flag) |
"hello world".includes("world"); // true
"hello world".startsWith("hello"); // true
"hello world".indexOf("o"); // 4
"hello".search(/l+/); // 2
"aabba".match(/a/g); // ["a", "a", "a"]
| Method | Returns | Description |
|---|
slice(start, end?) | String | Extract section (supports negative index) |
substring(start, end?) | String | Extract section (no negative index) |
at(index) | Character | Character at index (supports negative) |
charAt(index) | Character | Character at index |
charCodeAt(index) | Number | UTF-16 code unit |
codePointAt(index) | Number | Unicode code point |
"hello world".slice(0, 5); // "hello"
"hello world".slice(-5); // "world"
"hello".at(-1); // "o"
| Method | Returns | Description |
|---|
toUpperCase() | String | All uppercase |
toLowerCase() | String | All lowercase |
trim() | String | Remove whitespace both ends |
trimStart() | String | Remove leading whitespace |
trimEnd() | String | Remove trailing whitespace |
padStart(len, fill?) | String | Pad start to length |
padEnd(len, fill?) | String | Pad end to length |
repeat(n) | String | Repeat n times |
normalize(form?) | String | Unicode normalization |
" hello ".trim(); // "hello"
"5".padStart(3, "0"); // "005"
"ha".repeat(3); // "hahaha"
Replacing
| Method | Returns | Description |
|---|
replace(search, replacement) | String | Replace first match |
replaceAll(search, replacement) | String | Replace all matches |
"hello world".replace("world", "JS"); // "hello JS"
"aaa".replaceAll("a", "b"); // "bbb"
"abc 123".replace(/\d+/, "#"); // "abc #"
// With capture groups
"John Smith".replace(/(\w+) (\w+)/, "$2, $1"); // "Smith, John"
// With function
"hello".replace(/./g, c => c.toUpperCase()); // "HELLO"
Splitting & Joining
"a,b,c".split(","); // ["a", "b", "c"]
"a,b,c".split(",", 2); // ["a", "b"] (limit)
"hello".split(""); // ["h", "e", "l", "l", "o"]
["a", "b", "c"].join("-"); // "a-b-c"
Concatenating
"hello" + " " + "world"; // "hello world"
"hello".concat(" ", "world"); // "hello world"
`${a} ${b}`; // Template literal
Comparing
"a" < "b"; // true (lexicographic)
"a".localeCompare("b"); // -1 (locale-aware)
"a".localeCompare("A", undefined, { sensitivity: "base" }); // 0
Raw Strings & Escape Sequences
| Escape | Description |
|---|
\n | Newline |
\t | Tab |
\\ | Backslash |
\' | Single quote |
\" | Double quote |
\uXXXX | Unicode character |
\u{XXXXX} | Unicode code point |
String.raw`hello\nworld`; // "hello\\nworld" (no escape)
Iteration
for (const char of "hello") { /* ... */ }
[..."hello"]; // ["h", "e", "l", "l", "o"]
Common Recipes
// Capitalize first letter
s.charAt(0).toUpperCase() + s.slice(1);
// Truncate with ellipsis
s.length > n ? s.slice(0, n) + "..." : s;
// Count occurrences
(s.match(/pattern/g) || []).length;
// Remove all whitespace
s.replace(/\s/g, "");
// Slug from title
title.toLowerCase().replace(/\s+/g, "-").replace(/[^\w-]/g, "");
// Check if numeric
!isNaN(s) && !isNaN(parseFloat(s));