JavaScript strings have 30+ methods. This is a practical reference for the ones you'll use in interviews: searching, slicing, transforming, and pattern matching. Every method includes a runnable example.
1. Accessing Characters
charAt(index) / Bracket Notation [index]
Loading editor...
Difference: charAt returns '' for out-of-bounds; bracket notation returns undefined.
at(index) — Negative Index Support
Loading editor...
2. Searching
indexOf(substring, fromIndex?) / lastIndexOf(substring) — Find Position
Loading editor...
includes(substring, fromIndex?) — Contains Check
Loading editor...
startsWith(substring, fromIndex?) / endsWith(substring, length?) — Edge Checks
Loading editor...
3. Extracting Substrings
slice(start, end?) — Extract by Index
Loading editor...
Interview use: Getting all but the last part of a path, extracting segments.
substring(start, end) — Similar to Slice
Loading editor...
Doesn't support negative indices. slice is preferred.
split(separator, limit?) — String → Array
Loading editor...
Interview use: Path parsing in _.get() (#6), squashing/unsquashing keys (#16), multi-class splitting (#24).
4. Transforming
toLowerCase() / toUpperCase() — Case Conversion
Loading editor...
Interview use: Case-insensitive tag comparison in DOM traversal (#24).
trim() / trimStart() / trimEnd() — Whitespace Removal
Loading editor...
repeat(count) — Duplicate
Loading editor...
padStart(length, padString?) / padEnd(length, padString?) — Padding
Loading editor...
5. Replace & Match (Without Regex)
replace(substring, newValue) — Replace First Occurrence
Loading editor...
To replace all without regex, use replaceAll.
replaceAll(substring, newValue) — Replace All
Loading editor...
Interview use: Escaping special characters for JSON.stringify (#17), bracket-notation to dot-notation conversion (#16).
6. String Concatenation
concat(...strings) — Join Strings
Loading editor...
Template literals `${a} ${b}` are preferred for readability.
7. Regex-Based Methods
match(regex) — Find Matches
Loading editor...
Without the g flag, returns match details (groups, index). With g, returns all matches as an array.
search(regex) — Find Index of First Match
Loading editor...
replace(regex, replacement) — Regex Replace
Loading editor...
Interview use: Path parsing with dot/bracket notation (#6, #16), JSON string escaping (#17), bracket index extraction (#16).
Quick Reference
| Method | What It Does |
|---|---|
charAt(i) / [i] | Character at index |
at(i) | Character at index (supports negative) |
indexOf(s) | First occurrence index, or -1 |
lastIndexOf(s) | Last occurrence index |
includes(s) | Contains check (boolean) |
startsWith(s) | Starts with check |
endsWith(s) | Ends with check |
slice(s, e) | Extract substring by index |
split(sep) | Split into array |
toLowerCase() | Lowercase |
toUpperCase() | Uppercase |
trim() | Remove surrounding whitespace |
replace(s, r) | Replace first occurrence |
replaceAll(s, r) | Replace all occurrences |
repeat(n) | Repeat string n times |
padStart(n, s) | Pad from start |
padEnd(n, s) | Pad from end |
match(regex) | Regex match |
search(regex) | Regex search index |
concat(...s) | Concatenate |
Interview Tips
- Use
splitfor path parsing —'a.b.c'.split('.')is cleaner than manual iteration. - Use
replaceAllfor bulk replacement — no need for regex unless the pattern is complex. - Use
slicewith negatives —str.slice(-1)for last char,str.slice(0, -1)for all but last. - Case-insensitive comparison —
.toLowerCase()both strings before comparing.