JavaScript strings are immutable, so every method here returns a new string or a
derived value rather than changing the original. This reference lists each
String.prototype method with its signature, return type, and the unicode
gotchas that trip people up.
How it works
The methods group into a few jobs:
- Search —
indexOf,includes,startsWith,endsWith,match,searchlocate text. - Slice —
slice,substring,substr,atextract portions. - Transform —
toUpperCase,toLowerCase,trim,pad*,replace,replaceAll,repeat,normalizeproduce a new string. - Convert —
split,charAt,charCodeAt,codePointAt,localeComparemove between strings, arrays, and numbers.
Because strings are immutable, none of these mutate this; you always keep the
return value.
Tips
Prefer slice over substring/substr for predictable negative-index
behaviour. Use replaceAll for clear all-occurrence replacement and reserve
replace for the first match or regex captures. When comparing user text,
normalize('NFC') first so visually identical strings with different encodings
compare equal. Remember length counts UTF-16 code units — to count visible
characters iterate with for...of or [...str].length, which respect whole
code points.