Full answer
A string method never changes the string it is called on. It returns a new one, and the original stays as it was. Forgetting this is a classic beginner bug: text.trim() on its own line does nothing useful.
A working set of methods covers most tasks:
- Searching:
includes,startsWith,endsWith,indexOf(returns -1 when absent). - Cutting:
slice(start, end),at(-1)for the last character,split(separator). - Cleaning:
trim,trimStart,trimEnd,toLowerCase,toUpperCase. - Building:
repeat,padStart,padEnd(handy for fixed-width numbers such as'7'.padStart(3, '0')),concat, and template literals. - Replacing:
replacechanges only the first match for a string pattern, whilereplaceAllchanges every match.
Characters are stored as UTF-16 code units. Most letters take one unit, but many emoji take two (a surrogate pair), and an accented letter can be written as a base letter plus a combining mark. As a result, '\u{1F600}'.length is 2, and two strings that look identical can have different lengths. Array.from(text) or spread iterates by code point, which fixes emoji pairs. For what a reader counts as one character, including combined marks and family emoji, use Intl.Segmenter. normalize('NFC') makes the two forms of an accented letter equal before comparing.
For comparing text for humans, use localeCompare and not <.
const text = ' Learn JavaScript ';
text.trim();
console.log(JSON.stringify(text), JSON.stringify(text.trim()));
const course = 'JavaScript';
console.log(course.includes('Script'), course.startsWith('Java'), course.indexOf('x'));
console.log(course.slice(0, 4), course.slice(-6), course.at(-1), course[0]);
console.log('a-b-c'.replace('-', '+'), 'a-b-c'.replaceAll('-', '+'));
console.log('7'.padStart(3, '0'), 'ab'.repeat(3), 'x'.padEnd(3, '.') + '|');
console.log('red,green,blue'.split(',').map((color) => color.toUpperCase()).join(' / '));
console.log('\u{1F600}'.length, Array.from('\u{1F600}').length, [...'a\u{1F600}b'].length);
const composed = '\u00e9'; // one code unit
const decomposed = 'e\u0301'; // e plus a combining accent
console.log(composed === decomposed, composed.normalize('NFC') === decomposed.normalize('NFC'));
console.log(composed.length, decomposed.length);
const family = '\u{1F468}\u200D\u{1F469}\u200D\u{1F467}';
const segmenter = new Intl.Segmenter('en', { granularity: 'grapheme' });
console.log(family.length, [...family].length, [...segmenter.segment(family)].length);
console.log('a'.localeCompare('b'), ['z', 'ä', 'a'].sort((a, b) => a.localeCompare(b, 'de')));Run with Node.js v24.22.1 as js-52.js; two runs gave identical output. Output: " Learn JavaScript " "Learn JavaScript" true true -1 Java Script t J a+b-c a+b+c 007 ababab x..| RED / GREEN / BLUE 2 1 3 false true 1 2 8 5 1 -1 [ 'a', 'ä', 'z' ]
Key points
- Strings are immutable; methods return new strings.
- Know includes, slice, at, trim, split, replaceAll, padStart, and repeat.
- replace with a string pattern changes only the first match; replaceAll changes all.
- length counts UTF-16 code units, not visible characters.
- Use Array.from, Intl.Segmenter, and normalize for correct text handling.
Common mistakes
- Calling trim or toUpperCase and ignoring the returned string.
- Using replace with a string and expecting every match to change.
- Counting characters with length for text that may contain emoji or accents.
- Comparing user text without normalizing it first.
Follow-ups
- How do you reverse a string that contains emoji correctly?
- What is the difference between slice, substring, and the deprecated substr?
- How do you compare strings in a case- and accent-insensitive way?
Tips
- Say "strings are immutable" before listing any methods; it prevents the most common bug.
- Show the emoji length example. It is quick and memorable.
- Mention Intl.Segmenter for user-visible characters; few candidates know it.