slice() works like substring() with a few different behaviors.
Syntax: string.slice(start, stop);
Syntax: string.substring(start, stop);
What they have in common:
- If
startequalsstop: returns an empty string - If
stopis omitted: extracts characters to the end of the string - If either argument is greater than the string's length, the string's length will be used instead.
Distinctions of substring():
- If
start > stop, thensubstringwill swap those 2 arguments. - If either argument is negative or is
NaN, it is treated as if it were0.
Distinctions of slice():
- If
start > stop,slice()will return the empty string. ("") - If
startis negative: sets char from the end of string, exactly likesubstr(). - If
stopis negative: sets stop to:string.length – Math.abs(stop)(original value), except bounded at 0 (thus,Math.max(0, string.length + stop)) as covered in the ECMA specification.
Source: Rudimentary Art of Programming & Development: Javascript: substr() v.s. substring()
Answer from Daniel Vassallo on Stack Overflowslice() works like substring() with a few different behaviors.
Syntax: string.slice(start, stop);
Syntax: string.substring(start, stop);
What they have in common:
- If
startequalsstop: returns an empty string - If
stopis omitted: extracts characters to the end of the string - If either argument is greater than the string's length, the string's length will be used instead.
Distinctions of substring():
- If
start > stop, thensubstringwill swap those 2 arguments. - If either argument is negative or is
NaN, it is treated as if it were0.
Distinctions of slice():
- If
start > stop,slice()will return the empty string. ("") - If
startis negative: sets char from the end of string, exactly likesubstr(). - If
stopis negative: sets stop to:string.length – Math.abs(stop)(original value), except bounded at 0 (thus,Math.max(0, string.length + stop)) as covered in the ECMA specification.
Source: Rudimentary Art of Programming & Development: Javascript: substr() v.s. substring()
TL;DR:
- If you know the index (the position) on which you'll stop (but NOT include), use
slice(). - If you know the length of characters to be extracted, you could use
substr(), but that is discouraged as it is deprecated.
Otherwise, read on for a full comparison
Syntax
string.slice(start,end)string.substr(start,length)string.substring(start,end)
Note #1: slice()==substring()
What it does?
slice()extracts parts of a string and returns the extracted parts in a new string.substr()extracts parts of a string, beginning at the character at the specified position, and returns the specified number of characters.substring()extracts parts of a string and returns the extracted parts in a new string.
Note #2: slice()==substring()
Changes the Original String?
slice()doesn'tsubstr()doesn'tsubstring()doesn't
Note #3: slice()==substr()==substring()
Using Negative Numbers as an Argument
slice()selects characters starting from the end of the stringsubstr()selects characters starting from the end of the stringsubstring()doesn't perform
Note #4: slice()==substr()
If the First Argument is Greater than the Second
slice()doesn't performsubstr()since the Second Argument is NOT a position, but length value, it will perform as usual, with no problemssubstring()will swap the two arguments, and perform as usual
The First Argument
slice()required; starting Indexsubstr()required; starting Indexsubstring()required; starting Index
Note #5: slice()==substr()==substring()
The Second Argument
slice()optional; the position (up to, but not including) where to end the extractionsubstr()optional; the number of characters to extractsubstring()optional; the position (up to, but not including) where to end the extraction
Note #6: slice()==substring()
What if the Second Argument is Omitted?
slice()selects all characters from the start-position to the end of the stringsubstr()selects all characters from the start-position to the end of the stringsubstring()selects all characters from the start-position to the end of the string
Note #7: slice()==substr()==substring()
So, you can say that there's a difference between slice() and substr(), while substring() is basically a copy of slice().
If you want substr's functionality:
"foobarbaz".substr(index, length);
without using a deprecated feature, you can just do:
"foobarbaz".substring(index, length + index);
And get the exact same results bar all of the edge-cases, like negative index/length.
Videos
Even though it looks superficially like slice and substring do the same thing, the big difference is in how they handle negative arguments.
When JavaScript was first created in Netscape 2.0, there was just a substring method. If either of its arguments are negative, they are treated as 0.
When JavaScript 1.2 was introduced with Netscape 4.0, they wanted to add the behavior of allowing negative indexes to mean distances from the end of the string. They couldn't change substring to have this new behavior because it would break backward compatibility with scripts that expected negative indexes to be treated as 0, so they had to create a new function to support the added feature. This function was called slice, and was implemented on Array as well as String.
Another, smaller difference is that with substring the order of the arguments doesn't matter, so substring(1, 4) is the same as substring(4, 1). With slice, order does matter, so slice(4, 1) will just yield an empty string.
One item that makes them different is the second parameter that you have omitted
slice: the second parameter is the end index (exclusive) of the range to take.substr: the second parameter is the length of the string to take from the index specified with the first parameter
Can you completely replicate the behavior of one method with the other on string instances? Yes. Why they chose to include both is probably lost to history. My guess though would be familiarity. I bet there are very few frameworks out there which have slice for strings but plenty that have substr.