What is the difference between String.slice and String.substring?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
JavaScript offers both String.prototype.slice and String.prototype.substring for extracting part of a string. They look similar in simple cases, so teams often treat them as interchangeable. The important differences appear when indexes are negative, out of order, or derived from uncertain user input.
Shared Basics
Both methods return a new string and do not mutate the original value. Both use a start index and an optional end index, with the end being exclusive.
In straightforward index ranges, outputs are identical. Problems begin when indexes are less predictable.
How slice Handles Indexes
slice(start, end) supports negative indexes. A negative number means count from the end of the string.
This behavior is often exactly what you want when parsing suffixes, file extensions, or fixed-width trailers.
If start is greater than end, slice returns an empty string.
That strict behavior is useful when you want invalid ranges to fail quietly and clearly.
How substring Handles Indexes
substring(start, end) does not support negative indexes in the same way. Negative values are treated as zero. Also, if start is greater than end, the method swaps them.
This normalization can be convenient, but it can also hide indexing mistakes in upstream logic. A bug in index calculation may still produce non-empty output, which makes troubleshooting harder.
substring is still valid and fully supported, but you should use it deliberately when you want this forgiving behavior.
Behavior Comparison in Practical Scenarios
A side-by-side example makes the differences obvious.
The slice results are strict and directional. The substring results are normalized, which may or may not match your intent.
Choosing the Right Method
For most modern codebases, slice is the safer default because it is predictable and works naturally with negative offsets.
Use slice when:
- you need negative indexing from the end
- you want strict range behavior
- you are writing parser-style logic where index order matters
Use substring when:
- you intentionally want index normalization
- input ranges are user-controlled and you prefer forgiving extraction
- you are maintaining older code that already relies on this behavior
A clean helper can make intent explicit and reduce repeated mistakes.
Related Note on substr
Some developers still mention substr(start, length). It is legacy and not recommended for new code. If you are writing modern JavaScript, pick slice or substring and keep behavior explicit.
Common Pitfalls
- Assuming
substringsupports negative indexes likeslice. - Forgetting that
substringswaps out-of-order arguments. - Using
substringin parser logic where strict directional ranges are required. - Using mixed methods in one module, which increases cognitive load.
- Keeping legacy
substrexamples in new code and confusing future maintainers.
Summary
- Both methods return new strings and leave the original string unchanged.
slicesupports negative indexes and does not swap index order.substringtreats negative values as zero and swaps out-of-order indexes.sliceis usually the better default for modern, predictable code.- Pick one method per code area and document the choice for consistency.
Related reading
- What is the difference between substr and substring?
- What is the difference between synchronous and asynchronous programming in node.js
- What is the difference between visibility:hidden and display:none?
- What is the equivalent to a JavaScript setInterval/setTimeout in Android/Java?
- What is the formula to calculate the font-size for tags in a tagcloud?
- What is the (function() { } )() construct in JavaScript?
- What is the Haskell response to Node.js?
- What is the meaning of 'No bundle URL present' in react-native?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.