String Methods in JavaScript: slice vs substring

Easy100% Free~12 mins#strings#string-methods#immutability#slice#replace
Interview Answer (30-Second Summary)

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
Interactive Challenge

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.

Call Stack (Top = Active)
Global
Lexical Scope / Bindings
s:' Hello, World '
t:'Hello, World'
Console Stream
> 12 HELLO, WORLD

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.

  1. slice vs substring: both take start and end indexes. slice counts negative indexes from the end (slice(-5) is the last five characters); substring turns negative indexes into 0 and swaps the arguments if start is larger. at(-1) is the shortest way to read the last character.
  2. Searching: indexOf, lastIndexOf, includes, startsWith and endsWith are case-sensitive. Normalise with toLowerCase() first when case should not matter.
  3. Replacing and splitting: replace('l', 'L') changes only the first match; use replaceAll or a regex with the g flag for all of them. split(separator) returns an array, and join turns 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.

Q1.
What are string methods in JavaScript?

Functions every string inherits from String.prototype, such as slice, substring, split, indexOf, includes, replace, replaceAll, trim, toUpperCase, padStart, repeat and at. They read the string and return a new value.

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.

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'.

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.

Yes. 'Hello, World'.includes('world') is false. Lower-case both sides first, for example a.toLowerCase().includes(b.toLowerCase()).

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.

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