JavaScript String Methods

Every String.prototype method — signature, real examples that log their output, and the gotcha for each one. All strings in JS are immutable — every method returns a new string.

Search & Test

Transformation

String.prototype.replace
Returns a new string with the first match of pattern replaced. Pattern can be a string (only the first occurrence) or a RegExp (all occurrences when the g flag is set).
String.prototype.replaceAll
Returns a new string with every occurrence of pattern replaced. Added in ES2021; if pattern is a RegExp it must have the g flag or replaceAll throws TypeError.
String.prototype.toUpperCase
Returns a new string with all characters converted to upper case using the Unicode default case mapping. Locale-insensitive — use toLocaleUpperCase for language-specific rules (e.g. Turkish i).
String.prototype.toLowerCase
Returns a new string with all characters converted to lower case using Unicode default case mapping. For locale-specific behavior use toLocaleLowerCase.
String.prototype.trim
Returns a new string with whitespace removed from both ends. Whitespace includes spaces, tabs, newlines, and Unicode whitespace like non-breaking space.
String.prototype.trimStart
Returns a new string with leading whitespace removed. Added in ES2019 (aliased as trimLeft for legacy code).
String.prototype.trimEnd
Returns a new string with trailing whitespace removed. Added in ES2019 (aliased as trimRight for legacy code).
String.prototype.padStart
Returns a new string padded on the left with padString (default ' ') until it reaches targetLength. Added in ES2017; the original is returned unchanged if it is already long enough.
String.prototype.padEnd
Returns a new string padded on the right with padString (default ' ') until it reaches targetLength. Added in ES2017.
String.prototype.repeat
Returns a new string containing count copies of the original string concatenated together. Added in ES2015; count must be a non-negative finite integer.
String.prototype.normalize
Returns the Unicode Normalization Form of the string ('NFC', 'NFD', 'NFKC', or 'NFKD'; default 'NFC'). Essential for reliable comparison of characters that can be represented in multiple code point sequences.

Extraction & Access

Inspection

Conversion

Iteration

Related references