myArray.slice( start, [ end ] ) in ECMAScript 262
Return a specified section of an array.
Arguments
name | type | description |
---|---|---|
start | Number | The index to start copying fro |
end | Number |
[optional]
The index to copy up until. |
- Return Type
- Array
Description
A new array is created, and values from the original array are copied into it, starting at the start
index and up to (but not including) the end
index. If end
is omitted, the copying continues from start
until the end of the array.
If either start
or end
are negative, they are treated as offsets from the end of the array. For example:
var colors = [ 'black' , 'white' , 'orange' , 'green' , 'red' ];
var firstTwo = colors.slice(0,2); // ['black','white']
var secondPair = colors.slice(2,4); // ['orange','green']
var lastTwo = colors.slice(-2); // ['green','red']
var thirdFromTheEnd = colors.slice(-3,-2); // ['orange']
var allButTheLastTwo = colors.slice(0,-2); // ['black','white','orange'];
If the calculated end point is less than or equal to the calculated start point, an empty array is returned.