π§΅ Strings β JavaScript
Home: Interview Prep Index
JavaScript strings are immutable primitive values. All string methods return new strings β you cannot modify a string in place. For character-by-character work, convert to an array with split('') or Array.from(s).
Subtopics
| # | Topic | Description |
| 01 | String Basics | Creation, access, slicing, charCode, template literals |
| 02 | JS String Methods | indexOf, slice, split, includes, replaceAll and more |
| 03 | Patterns | Two pointers, sliding window, palindromes, stack |
| 04 | Top 25 Interview Questions | Solved problems with 5-step approach sections |
The Big Picture
STRINGS IN JAVASCRIPT
β
βββ Basics β length, access, slice, charCode
β βββ s.length
β βββ s[i], s.charAt(i)
β βββ s.slice(start, end)
β βββ s.charCodeAt(i), String.fromCharCode(n)
β
βββ Built-in Methods β JS standard library
β βββ indexOf / lastIndexOf / includes
β βββ startsWith / endsWith
β βββ toUpperCase / toLowerCase
β βββ trim / trimStart / trimEnd
β βββ split / join (array method)
β βββ replace / replaceAll
β βββ repeat / padStart / padEnd
β βββ match / matchAll / search
β
βββ Patterns β DSA on strings
β βββ Two Pointers β reverse, palindrome
β βββ Sliding Window β longest no-repeat, min window
β βββ Hashing β anagram, frequency count
β βββ Stack β valid parentheses, decode
β
βββ Interview Questions β Top 25 problems
Complexity Cheat Sheet
| Operation | Time | Notes |
s.length | O(1) | Cached property |
s[i] | O(1) | Direct access |
s + t (concat) | O(n+m) | New string created |
s.slice(i,j) | O(j-i) | |
s.indexOf(t) | O(nΓm) | Naive search |
s.split('') | O(n) | Creates array |
arr.join('') | O(n) | |
s.repeat(k) | O(nΓk) | |
PHP vs JS String Syntax
| Operation | PHP | JavaScript |
| Length | strlen($s) | s.length |
| Access char | $s[$i] | s[i] or s.charAt(i) |
| Substring | substr($s, 1, 3) | s.slice(1, 4) |
| Find | strpos($s, 'x') | s.indexOf('x') |
| Reverse | strrev($s) | s.split('').reverse().join('') |
| Split | str_split($s) | s.split('') or Array.from(s) |
| Join | implode('', $arr) | arr.join('') |
| Upper | strtoupper($s) | s.toUpperCase() |
| Contains | str_contains($s, 'x') | s.includes('x') |
| Starts with | str_starts_with | s.startsWith('x') |
| Ends with | str_ends_with | s.endsWith('x') |
| ASCII | ord($c) | s.charCodeAt(i) |
| Char from int | chr($n) | String.fromCharCode(n) |
| Repeat | str_repeat($s, 3) | s.repeat(3) |
| Pad | str_pad($s, 5, '0', STR_PAD_LEFT) | s.padStart(5, '0') |