substr vs substring in JavaScript
Intermediate · 3 min read
If you read older JavaScript code, you will meet substr. It looks almost exactly like substring, and many people assume they are the same method with a shorter name. They aren't. The second number means something completely different.
In this tutorial you will see what substr(start, length) does, compare it with substring and slice, and learn to rewrite any substr call with slice. You should already know slice(start, end): the start is included and the end is left out.
Index or length?
There are three methods for cutting text, and they disagree about the second number:
slice(start, end): the second number is where to stop.substring(start, end): the second number is also where to stop.substr(start, length): the second number is how many characters you want.
That is the whole difference, and the whole source of confusion. Here are the positions of 'JavaScript' to work with:
J a v a S c r i p t
0 1 2 3 4 5 6 7 8 9
An old method that is still around
substr is listed in a special part of the JavaScript rules for old features that browsers keep so old websites don't break. It still works everywhere, but it is not recommended for new code. You need to understand it to read code, not to write it.
Here is the exercise. Have a go yourself first, or read on and we will build it together.
7 substr, the legacy one
fairsubstr(start, length) takes a length as its second argument, not an end index. Show what that changes, and write the same call with slice.
Stuck? Three hints, smallest first
- 1. The second argument is how many characters you want, not where to stop.
- 2. So
substr(4, 3)isslice(4, 4 + 3). - 3. It is in Annex B of the spec — kept for compatibility, not recommended for new code.
Step 1: three characters from position 4
const text = 'JavaScript';
const byLegacy = text.substr(4, 3);
console.log(byLegacy); // 'Scr'
substr(4, 3) starts at position 4, the S, and takes three characters: S, c, r.
Step 2: the same thing with slice
slice wants a stop position, not a count. Three characters starting at 4 stop just before 7, so the stop is start + length.
const text = 'JavaScript';
const bySlice = text.slice(4, 4 + 3);
console.log(bySlice); // 'Scr'
console.log(bySlice === text.substr(4, 3)); // true
That one rule converts any substr call: text.substr(start, n) becomes text.slice(start, start + n).
Step 3: leave the length off
With no second number, substr runs to the end, just like slice.
const text = 'JavaScript';
console.log(text.substr(4)); // 'Script'
console.log(text.slice(4)); // 'Script'
Step 4: a negative start
A negative start counts from the right, as it does in slice. -6 in a 10-character string means position 4.
const text = 'JavaScript';
console.log(text.substr(-6, 3)); // 'Scr'
Step 5: a zero or negative length
You can't have a negative number of characters, so both give an empty string.
const text = 'JavaScript';
console.log(JSON.stringify(text.substr(4, 0))); // "", an empty string
console.log(JSON.stringify(text.substr(4, -1))); // "", an empty string
JSON.stringify is only there so the empty strings are visible when printed.
Step 6: the mix-up with substring
Now the mistake this exercise is really about. Someone reads substr(4, 3), thinks "that's the same as substring", and writes:
const text = 'JavaScript';
console.log(text.substr(4, 3)); // 'Scr'
console.log(text.substring(4, 3)); // 'a'
substring(4, 3) reads the 3 as a stop position. The start, 4, is bigger than the stop, 3, so substring quietly swaps them and runs substring(3, 4). That is just position 3, the letter a. Same numbers, a completely different answer, and no error to warn you.
Step 7: it is still a working function
console.log(typeof 'JavaScript'.substr); // 'function'
substr hasn't been removed, so old code using it keeps working. TypeScript's built-in type definitions mark it as deprecated, "a legacy feature for browser compatibility", which is why editors that use them show it crossed out.
The full solution
Here is the complete answer: the substr call, its slice twin, and the substring mix-up with the same numbers. Press Run and compare the first two lines.
const text = 'JavaScript';
// The whole difference is in the second argument.
const byLegacy = text.substr(4, 3); // start 4, three characters
const bySlice = text.slice(4, 4 + 3); // start 4, stop before 7
console.log('substr(4, 3) :', byLegacy);
console.log('slice(4, 7) :', bySlice);
console.log('substr(4) :', text.substr(4));
console.log('substr(4, -1):', JSON.stringify(text.substr(4, -1)), '<- a negative length gives nothing');
Output
9 checks that prove this answer
You do not have to read these. They are part of the answer panel above, so pressing
Run there executes all 9 of them and prints ok for each.
That output is the proof, and the build fails if a single check fails.
test('substr takes a LENGTH, not an end index', text.substr(4, 3), 'Scr');
test('the slice equivalent needs start + length', text.slice(4, 4 + 3), 'Scr');
test('so the two agree once you convert', byLegacy, bySlice);
test('with no length it runs to the end', text.substr(4), 'Script');
test('a negative start counts from the right, like slice', text.substr(-6, 3), 'Scr');
test('a zero length gives an empty string', text.substr(4, 0), '');
test('and so does a negative length', text.substr(4, -1), '');
test('substring with the same numbers means something else entirely', text.substring(4, 3), 'a');
test('it is still a real function, just a deprecated one', typeof text.substr, 'function');
Three methods, three meanings for the second argument: slice and substring take an end index, substr takes a count. That is the entire source of the confusion, and it is why text.substring(4, 3) above returns 'a' — it swapped the arguments to (3, 4) and gave you index 3.
substr is not going away, because too much code uses it, but it is in the compatibility annex rather than the main specification. Write slice.
Common mistakes
- Treating
substrandsubstringas the same. The second number is a count in one and a stop position in the other. - Converting
substr(a, n)toslice(a, n). The right conversion isslice(a, a + n). - Writing new code with
substr. It works, but it is an old feature kept for old pages. Useslice.
Try it yourself
- Rewrite
'2025-04-09'.substr(5, 2)withslice. What does it give? - Rewrite
'invoice.pdf'.substr(-3)withslice. - Predict, then check:
'Lagos'.substr(1, 3)and'Lagos'.substring(1, 3).