slice()
works as substring()
with some different behaviors.
Syntax
string.slice (start, stop);
string.substring (start, stop);
What they have in common:
If start is equal to stop: returns empty string
If stop is omitted: Extract characters to the end of the string
If one of the arguments is greater than the length of the string, the length of the string will be used instead.
Distinctions from substring () :
If start > stop, then the substring will exchange these 2 arguments.
If one of the arguments is negative or is NaN, it will be treated as 0.
Distinctions of slice () :
If start > stop, slice () will NOT change the 2 arguments.
If start is negative: set char at the end of the string, just like substr () in Firefox. This behavior is observed in Firefox and IE.
If stop is negative: set stop to: string.length - Math.abs (stop) (original value), except limited to 0 (therefore Math.max (0, string.length + stop)) as covered in the ECMA specification.
Font