The String .slice() method allows you to use a negative index:
var str = "319CDXB".slice( -3 );
EDIT:. To put it a bit, the .slice() method for String is a method that is very similar to its Array .
The first parameter represents the starting index, and the second is the index representing the breakpoint.
Any parameter allows you to use a negative index if the range makes sense. Omitting the second parameter means the end of the line.
Example: http://jsfiddle.net/patrick_dw/N4Z93/
var str = "abcdefg"; str.slice(0); // "abcdefg" str.slice(2); // "cdefg" str.slice(2,-2); // "cde" str.slice(-2); // "fg" str.slice(-5,-2); // "cde"
Another nice thing about .slice() is that it is widely supported in all major browsers. These two reasons make this (in my opinion) the most attractive option for getting a section of a string.
user113716
source share