| Refresh | Home EGTry.com

get subarray


syntax

anArray.slice(beginIndex, endIndex_not_included);


commentsJavascript sourceExecution result in your current browser
initialize an array
var a1=[1,2,3,4,5];
document.write(a1);

return the first 3 elements
document.write(a1.slice(0,3));

return all elements after 3th elements
document.write(a1.slice(3));

return the last 3 elements
document.write(a1.slice(-3));

copy array
var newArray=a1.slice();
newArray[0]="1111";
document.write("original array: "+a1+"<br/>");
document.write("the copy: "+newArray+"<br/>");