String Methods in JavaScript: slice vs substring
String methods in JavaScript, such as slice, substring, split, indexOf, replace, replaceAll, trim, includes and at, all return a new value: strings are immutable, so no method changes the original string. The classic traps are slice vs substring with negative indexes, replace changing only the first match, and case-sensitive searches.
Key Learning Objectives
Use the common string methods: slice, substring, split, indexOf, includes, replace, replaceAll, trim and at.
Explain why strings are immutable and why a method call must be assigned.
Predict slice vs substring with negative indexes, and replace vs replaceAll.
Avoid case-sensitivity and out-of-range traps.
The Interview Problem
What do the six console.log calls print?
1const s = ' Hello, World ';2const t = s.trim();3console.log(t.length, t.toUpperCase());4console.log(t.slice(-5), t.substring(-5, 5));5console.log(t.split(', ').length, t.indexOf('o'), t.lastIndexOf('o'));6console.log(t.replace('l', 'L'), t.replaceAll('l', 'L'));7console.log(t.at(-1), t.includes('world'));8t.toUpperCase();9console.log(t);
Predict Console Output
Select the option that matches what this code prints to the console:
12 HELLO, WORLD / World Hello / 2 4 8 / HeLlo, World HeLLo, WorLd / d false / Hello, World
12 HELLO, WORLD / World Hello / 2 4 8 / HeLlo, World HeLLo, WorLd / d false / HELLO, WORLD
12 HELLO, WORLD / World World / 2 4 8 / HeLlo, World HeLLo, WorLd / d false / Hello, World
12 HELLO, WORLD / World Hello / 2 4 8 / HeLLo, WorLd HeLLo, WorLd / d false / Hello, World
V8 Engine Execution Trace
Step 1 of 6 (Line 3)trim returns a new string without the outer spaces: 12 characters. toUpperCase returns another new string.
String Methods in JavaScript Explained
How String Methods in JavaScript Behave
String methods in JavaScript live on String.prototype, so every string can call them. They share one rule: strings are immutable, so each method returns a new value and the original stays the same. t.toUpperCase(); on its own line does nothing useful.
- slice vs substring: both take start and end indexes.
slicecounts negative indexes from the end (slice(-5)is the last five characters);substringturns negative indexes into 0 and swaps the arguments if start is larger.at(-1)is the shortest way to read the last character. - Searching:
indexOf,lastIndexOf,includes,startsWithandendsWithare case-sensitive. Normalise withtoLowerCase()first when case should not matter. - Replacing and splitting:
replace('l', 'L')changes only the first match; usereplaceAllor a regex with thegflag for all of them.split(separator)returns an array, andjointurns an array back into a string.
Common Traps & Mistakes
Calling a string method without assigning the result and expecting the string to change.
Using substring with a negative index and expecting it to count from the end.
Using replace to change every match; it only changes the first unless the regex has the g flag.
FAANG Follow-Up Probes
Probe #1
Write a function that checks whether a string is a palindrome, ignoring case and punctuation.
Probe #2
Why does [...'👍a'].length differ from '👍a'.length?
Probe #3
How would you capitalise the first letter of every word in a sentence?
String Methods in JavaScript Interview Questions
Key interview inquiries, edge cases, and architectural follow-ups for String Methods.
Do string methods change the original string?
No. Strings are immutable primitives, so every method returns a new string (or number, boolean, array). That is why calling t.toUpperCase() on its own line leaves t as 'Hello, World'; you must assign the result.
What is the difference between slice and substring?
Both return the part between two indexes, but slice counts negative indexes from the end, while substring treats negative indexes as 0 (and swaps the arguments if start is larger). t.slice(-5) is 'World'; t.substring(-5, 5) is 'Hello'.
What is the difference between replace and replaceAll?
replace with a string pattern changes only the first match: 'Hello, World'.replace('l', 'L') is 'HeLlo, World'. replaceAll changes every match, and replace with a regex needs the g flag to do the same.
Are includes, indexOf and startsWith case-sensitive?
Yes. 'Hello, World'.includes('world') is false. Lower-case both sides first, for example a.toLowerCase().includes(b.toLowerCase()).
What does at() do on a string?
at(i) returns the character at index i and accepts negative indexes, so t.at(-1) is the last character. Out-of-range indexes give undefined, while charAt gives an empty string.
How do you reverse a string in JavaScript?
Strings have no reverse method, so convert to an array and back: [...str].reverse().join(''). Spreading the string, rather than split(''), keeps characters such as emoji in one piece.
More JavaScript interview questions for freshers
