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
String.prototype.indexOf
Returns the index of the first occurrence of searchValue within the string, or -1 if not found. Search is case-sensitive and starts from fromIndex (default 0).
String.prototype.includes
Returns true if searchString appears anywhere in the string, otherwise false. Added in ES2015 as a cleaner boolean alternative to indexOf() !== -1.
String.prototype.startsWith
Returns true if the string begins with searchString at the given position (default 0). Added in ES2015 and is more efficient than slice() + === comparisons.
String.prototype.endsWith
Returns true if the string ends with searchString when considered up to endPosition (default str.length). Useful for file extension and suffix checks.
String.prototype.match
Runs a regular expression against the string and returns match information. With the g flag it returns an array of all match strings; without it, an exec-style array with capture groups, index, and input (or null).
String.prototype.matchAll
Returns an iterator of all detailed match results, each including capture groups, index, and input. Added in ES2020 and requires a global (g) RegExp — non-global patterns throw TypeError.
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
String.prototype.slice
Extracts a section of the string from beginIndex up to but not including endIndex, returning a new string. Negative indices count from the end.
String.prototype.substring
Returns a new string between indexStart and indexEnd (exclusive). Unlike slice(), negative arguments are treated as 0 and the arguments are swapped when indexStart > indexEnd.
String.prototype.substr
Returns a portion of the string starting at start with the specified length. Deprecated as a legacy web feature — new code should use slice or substring, but implementations still ship it.
String.prototype.at
Returns the UTF-16 code unit at the given index as a single-character string, supporting negative indices that count from the end. Added in ES2022 as a cleaner alternative to str[str.length - 1].
String.prototype.codePointAt
Returns the full Unicode code point (0–0x10FFFF) starting at the given position, correctly combining surrogate pairs. Added in ES2015 as the Unicode-safe counterpart to charCodeAt.
Inspection
String.prototype.length
Read-only property giving the number of UTF-16 code units in the string. Not the number of visible characters — emoji and combining marks can inflate the count.
String.prototype.localeCompare
Returns a negative number, 0, or a positive number indicating whether the string sorts before, equal to, or after compareString according to the current or supplied locale. Preferred over < and > for user-visible sorting.