Javascript slice


JS Array.slice(a,b) method returns a subarray between index a and b. The method does not modify the array.

var arr = new Array(1,2,3,4,5);
var ret = arr.slice(2,4);  //3,4

If only one argument is provided, the subarray will be the specified postion to the end of array.

var arr = new Array(1,2,3,4,5);
var ret = arr.slice(2);  //3,4,5

The argument with negative value represents its position relative to the last element.

var arr = new Array(1,2,3,4,5);
var ret = arr.slice(2,-1);  //3,4
ret = arr.slice(-2,-1);  //4

String.substr() method gets a substring of a target string:

var str = "javascript endmemo";
alert(str.substr(5)); //cript endmemo
alert(str.substr(5,10)); //cript endm