JavaScript String is a primitive, immutable, UTF-16 encoded sequence of code units.
A companion String object wrapper provides methods for inspection, slicing, searching, transformation, comparison, and Unicode operations.
Strings are immutable: all operations return new strings.
const a = "hello";
const b = 'hello';
const c = `hello ${1 + 2}`;
const d = String("hello");
const e = new String("hello");
const s = "Aditya";
s[0]; // "A"
s.length; // 6
Strings cannot be mutated.
charAt(index) โ Returns the character at the specified index."aditya".charAt(2); // "i"
charCodeAt(index) โ Returns UTF-16 code unit at index."ABC".charCodeAt(0); // 65
codePointAt(index) โ Returns full Unicode code point."๐".codePointAt(0); // 66304
at(index) โ Returns character at index; supports negative indexing."aditya".at(-1); // "a"
indexOf(sub, fromIndex?) โ Returns first index of sub, or -1 if not found."banana".indexOf("na"); // 2
lastIndexOf(sub, fromIndex?) โ Returns last index of sub."banana".lastIndexOf("na"); // 4
includes(sub, fromIndex?) โ Returns true if substring exists."javascript".includes("script"); // true
startsWith(prefix, pos?) โ Checks if string begins with prefix."aditya".startsWith("adi"); // true
endsWith(suffix, length?) โ Checks if string ends with suffix."aditya".endsWith("tya"); // true
search(regex) โ Returns index of first regex match."hello123".search(/\d/); // 5
match(regex) โ Returns array of matches or null."abc123".match(/\d+/); // ["123"]
matchAll(regex) โ Returns iterator over all regex matches.[... "a1b2c3".matchAll(/\d/g)];
slice(start, end) โ Extracts substring; supports negative indices."javascript".slice(0, 4); // "java"
substring(start, end) โ Extracts substring; swaps reversed indices."javascript".substring(4, 0);
substr(start, length) โ Extracts substring of given length (legacy)."javascript".substr(4, 6);
toUpperCase() โ Returns uppercased copy."adi".toUpperCase();
toLowerCase() โ Returns lowercased copy."ADI".toLowerCase();
trim() โ Removes leading & trailing whitespace." x ".trim();
trimStart() / trimEnd() โ Removes leading or trailing whitespace." x ".trimStart();
repeat(n) โ Repeats string n times."ha".repeat(3);
replace(searchValue, replaceValue) โ Replaces first match (or regex)."banana".replace("a", "A");
replaceAll(searchValue, replaceValue) โ Replaces all occurrences."banana".replaceAll("a", "A");
padStart(targetLength, padString?) โ Pads the start to target length."5".padStart(3, "0");
padEnd(targetLength, padString?) โ Pads the end to target length."5".padEnd(3, "0");
split(separator, limit?) โ Splits string into array."1,2,3".split(",");
for...of โ Iterates by Unicode code points.for (const ch of "aditya") console.log(ch);
String.prototype[Symbol.iterator] โ Underlying iterator.`
Hello
World
`
`Total: ${2 + 3}`;
tag`line1\nline2`;
normalize(form) โ Normalizes Unicode strings."\u1E9B\u0323".normalize("NFC");
"๐".length; // 2
[... "๐"].length; // 1
toString() โ Converts value to string.(123).toString();
valueOf() โ Returns primitive string of wrapper.new String("abc").valueOf();
localeCompare() โ Compares strings using locale rules."รค".localeCompare("z", "de");
| Category | Methods |
|---|---|
| Character | charAt, charCodeAt, codePointAt, at |
| Search | indexOf, lastIndexOf, includes, startsWith, endsWith, match, matchAll, search |
| Extraction | slice, substring, substr |
| Transformation | toUpperCase, toLowerCase, trim*, repeat, replace*, padStart, padEnd |
| Splitting | split |
| Unicode | normalize |
| Locale | localeCompare |
| Iteration | Symbol.iterator, for...of |
| Conversion | toString, valueOf |
All string operations create new strings.
slice vs. substringslice.substring swaps arguments.UTF-16 code units cause "๐".length === 2.
"123".replace(/\d+/g, m => m * 2);
" aDiTyA "
.trim()
.toLowerCase()
.replaceAll("a","@")
.padStart(10,"_");
function sub(s,a,b){
let r="";
for(let i=a;i<b;i++) r+=s[i];
return r;
}
(Your included image remains valid.)
"๐".length === 2?slice, substring, substr.charCodeAt vs codePointAt.new String()?localeCompare provide?matchAll differ from match?Intl.Segmenter)If you want, I can also export this as PDF, Markdown file, Notion-friendly format, or interview cheat sheet.