The JavaScript substring() method returns the part of a string between a start index and an end index. The character at the start index is included and the character at the end index is not. If you leave out the end index, it returns everything from the start index to the end of the string. It returns a new string and leaves the original unchanged.
const word = 'Mozilla';
word.substring(0, 6); // 'Mozill'
word.substring(4); // 'lla'Syntax
substring(indexStart)
substring(indexStart, indexEnd)indexStartis the index of the first character to include. Indexes start at 0.indexEndis the index of the first character to leave out. It is optional. Without it, the result runs to the end of the string.
substring() examples
These results all use the string 'Mozilla':
const str = 'Mozilla';
str.substring(0, 1); // 'M'
str.substring(1, 0); // 'M'
str.substring(0, 6); // 'Mozill'
str.substring(4); // 'lla'
str.substring(7, 4); // 'lla'
str.substring(0, 10); // 'Mozilla'Notice that substring(1, 0) and substring(7, 4) still return text, and that an end index past the length of the string is simply treated as the length.
How substring() handles unusual arguments
substring() does not throw for out-of-range indexes. It adjusts them instead:
If
indexStartequalsindexEnd, the result is an empty string.If
indexStartis greater thanindexEnd, the two arguments are swapped.Negative values are treated as 0, and values greater than the string length are treated as the length.
NaNis treated as 0.
const str = 'Mozilla';
str.substring(-5, 2); // 'Mo' (-5 becomes 0)
str.substring(-5, -2); // '' (both become 0)The swapping behaviour is the one that surprises people. It means a bug that passes the arguments in the wrong order will not fail. It will quietly return the wrong text.
substring() vs slice()
slice() takes the same two arguments, but it handles them differently:
A negative index in
slice()counts back from the end of the string. Insubstring()it becomes 0.If the start is after the end,
slice()returns an empty string.substring()swaps the arguments.
const text = 'Mozilla';
text.substring(5, 2); // 'zil'
text.slice(5, 2); // ''Negative indexes are what make slice() more useful in everyday code:
const str = 'The morning is upon us.';
str.slice(-3); // 'us.'
str.slice(-3, -1); // 'us'
str.slice(0, -1); // 'The morning is upon us'
str.slice(4, -1); // 'morning is upon us'For that reason, slice() is usually the better default. Use substring() when you want the clamping and swapping behaviour, or when you are reading code that already uses it.
substr() is deprecated
You may see substr() in older code. Its second argument is a length, not an end index. MDN marks it as deprecated and says to avoid using it in new projects. It still works in browsers today, but there is no reason to write new code with it.
// Old: substr(start, length)
'Mozilla'.substr(1, 3);
// Replacement for a non-negative start: slice(start, start + length)
'Mozilla'.slice(1, 1 + 3);Both lines return 'ozi'. The replacement shown here is for a start index that is zero or higher. A negative start behaves differently between the two methods, so check those cases by hand if you are converting old code.
Common tasks
Get the first n characters
const n = 5;
'JavaScript'.slice(0, n); // 'JavaS'Remove the last character
'Hello!'.slice(0, -1); // 'Hello'Get the text after a character
Combine indexOf() with substring():
const email = '[email protected]';
email.substring(email.indexOf('@') + 1); // 'example.com'Shorten a long string
function truncate(text, max) {
return text.length > max ? text.slice(0, max).trimEnd() + '…' : text;
}
truncate('A very long article title', 10); // 'A very lon…'Things to watch for
Indexes count UTF-16 code units. Characters outside the basic range, such as many emoji, take two units. Cutting a string in the middle of one produces a broken character. Use
Array.from(str)or the spread operator if you need to work with whole code points.slice(-n)withn = 0returns the whole string, because-0is the same as0. Handle that case separately ifncan be zero.Strings are immutable. None of these methods change the original string. Assign the result to a variable.
Using substring() in TypeScript
substring() works the same way in TypeScript. It takes numbers and returns a string, so no extra typing is needed:
const title: string = 'TypeScript';
const start: string = title.substring(0, 4); // 'Type'Frequently asked questions
Does substring() change the original string?
No. It returns a new string. JavaScript strings cannot be modified in place.
What is the difference between substring() and slice()?
slice() accepts negative indexes that count from the end and returns an empty string when the start is after the end. substring() treats negatives as 0 and swaps the arguments when the start is after the end.
Should I use substr()?
No. It is deprecated. Use slice() or substring() instead.





