slice (Array - JavaScript)

Gets elements from an array to form another array.

Defined in

Array (Standard - JavaScript)

Syntax

slice(start:int, end:int) : Array (Standard - JavaScript)
Parameters Description
start Starting element, inclusive, where 0 is the first element. A negative specification means the length of the array minus the absolute value of the specification. A negative result means 0.
end Ending element, exclusive, where 0 is the first element. Defaults to the position following the last element. A negative specification means the length of the array minus the absolute value of the specification. A negative result means 0.
Return value Description
Array Elements from start (inclusive) through end (exclusive). Returns an empty array if end does not exceed start.

Examples

(1) This computed label gets all elements of an array starting at the second element.
var a = new Array("one", "two", "three", "four");
a.slice(1) // [two, three, four]
(2) This computed label gets the second and third elements of an array.
var a = new Array("one", "two", "three", "four");
a.slice(1,3) // [two, three]
(3) This computed label gets the last element of an array.
var a = new Array("one", "two", "three", "four");
a.slice(-1) // [four]
(4) This computed label gets all elements of an array except the last.
var a = new Array("one", "two", "three", "four");
a.slice(0, -1) // [one, two, three]