
substr() selects characters starting from the end of the string. slice() selects characters starting from the end of the string. Note #3: slice()=substr()=substring() Using Negative Numbers as an Argument Note #2: slice()=substring() Changes the Original String? substring() 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. slice() extracts parts of a string and returns the extracted parts in a new string. Note #1: slice()=substring() What it does? Otherwise, read on for a full comparison Syntax If you know the length of characters to be extracted, you could use substr(), but that is discouraged as it is deprecated. If you know the index (the position) on which you'll stop (but NOT include), use slice(). Source: Rudimentary Art of Programming & Development: Javascript: substr() v.s. If stop is 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. This behavior is observed in both Firefox and IE. If start is negative: sets char from the end of string, exactly like substr() in Firefox.
If start > stop, slice() will return the empty string.If either argument is negative or is NaN, it is treated as if it were 0.If start > stop, then substring will swap those 2 arguments.If either argument is greater than the string's length, the string's length will be used instead.If stop is omitted: extracts characters to the end of the string.If start equals stop: returns an empty string.Slice() works like substring() with a few different behaviors.