JavaScript String Methods Table
| Method | Returns | Example | Note |
|---|---|---|---|
slice(7, -1) | New string | 'curly braces'.slice(7, -1) → 'brace' | Negative indexes count from the end |
substring(4, 9) | New string | 'javascript'.substring(4, 10) → 'script' | No negatives - swaps args if start > end |
at(-1) | Character | 'regex'.at(-1) → 'x' | Cleaner than slice for the last item |
split(',') | Array | 'a,b,c'.split(',') → ['a','b','c'] | The string-to-array bridge |
replace(/x/g, 'y') | New string | 'axa'.replace(/x/g,'y') → 'aya' | String pattern replaces only the first match |
replaceAll('x', 'y') | New string | 'axa'.replaceAll('x','y') → 'aya' | The readable global replace |
trim() / trimStart() / trimEnd() | New string | ' hi '.trim() → 'hi' | Whitespace per the spec - includes NBSP |
padStart(5, '0') | New string | '42'.padStart(5,'0') → '00042' | IDs, clocks, fixed-width columns |
includes('sub') | boolean | 'substring'.includes('sub') → true | Prefer over indexOf() >= 0 for reads |
indexOf('b') | Number | 'abc'.indexOf('b') → 1 | -1 means absent; also finds repeats in a loop |
toLowerCase() / toUpperCase() | New string | 'MiXeD'.toLowerCase() → 'mixed' | Locale variants take a locale argument |
startsWith('a') / endsWith('c') | boolean | 'abc'.startsWith('a') → true | Takes an offset for the search start |
repeat(3) | New string | 'ab'.repeat(3) → 'ababab' | Zero and negative zero give the empty string |
charAt(1) / [1] | Character | 'abc'.charAt(1) → 'b' | Bracket access is newer; charAt never returns undefined |
s.toUpperCase() silently does nothing unless you assign the result. That single fact explains most beginner bugs on this table. Reference: the MDN String guide. Bottom line: split gets you into arrays, join gets you back, and everything else is selection and reshaping in between. Related tools: JS array methods table for the other half, find and replace for bulk edits, remove accents and remove emoji for the cleaning jobs these methods automate.Strings are the data type every script touches first, and JavaScript answers with a long, mostly well-designed method set. The table below is the working fourteen - not the full catalog, the ones real code reaches for - each with what it returns, a runnable example, and the field note that saves a debugging session.
Bottom line: every method on this table returns a NEW string. JavaScript strings are immutable - calling s.toUpperCase() without assigning the result changes nothing, and that single fact explains most of the silent bugs beginners hit with this API. Chain methods freely; just remember the chain starts from a value, it never mutates one.
The honest part: three pairs confuse everyone, so the table disambiguates them directly. slice vs substring (only slice takes negative indexes), replace vs replaceAll (only replaceAll is global by default), and charAt vs bracket access (only brackets can return undefined on an out-of-range index).
How to use
- Find the intent in the first column - the method signature is the search key.
- Check the Returns column before chaining: booleans end chains, strings continue them, numbers go to arithmetic.
- Steal the example - each one is a complete expression you can paste into the browser console and verify in five seconds.
Frequently asked questions
What is the difference between slice and substring?
Two differences, one practical. Negative indexes: slice(-3) counts from the end, substring treats a negative as 0. Argument order: substring silently swaps its arguments if the start is bigger than the end; slice returns the empty string. In new code, slice does everything substring does plus negatives - reach for substring only when reading old code that already uses it.
Why does my replace only change the first match?
Because a string pattern replaces exactly once - that is the ECMAScript rule. Global replacement needs either a regular expression with the g flag (replace(/x/g, 'y')) or the newer replaceAll('x', 'y'), which reads exactly like the intent. The catch: replaceAll requires a global regex if you pass one, so replaceAll(/x/, 'y') throws - the g flag is mandatory there.
How do I split a string into an array and back?
split(',') cuts a string into an array on a separator; join('-') glues an array back into a string. The pair is the standard bridge between string world and array world - normalize with split, work with the array methods, rebuild with join. Calling split('') with an empty separator explodes the string into single characters, which is the quick way to count or filter letters.
Which string method should I use to trim whitespace?
trim() for both ends, trimStart() and trimEnd() when only one side matters. All three follow the ECMAScript whitespace definition, which includes more than the space character - tabs, and the invisible non-breaking space (U+00A0) that copy-paste from web pages leaves behind. That is why pasted text sometimes will not trim by hand: trim() handles it, backspace-hunting usually does not.