JavaScript substring: The Difference between Slice, Substring, and Substr

In JavaScript, you have 3 options for taking the substring of another String.

Let’s use the example String ‘dorchester’, and you want to extract ‘chest’, which goes from index 3 to index 7.

Substring and Slice

The syntax for both of these is the same:

  1. substring(startIndex [, endIndex])
  2. slice(startIndex [, endIndex])

EndIndex is not inclusive, so you usually want to do endIndex + 1.

To extract “chest”, we would run s.substring(3, 8) or s.slice(3, 8).


The syntax for substr is:

  • substr(startIndex [,count])

Instead of the endIndex, you specify how many characters after the start you want to include.

Because “chest” is 5 characters, we want to use 5.

So the proper syntax would be s.substr(3, 5).