Web Development Course:
- The original array is not affected by this method: a new one is created instead.
This JS slice function selects elements starting at the one indexed by start argument, and finishes at (but does not include) the element indexed by the end argument:
var carArray =['Mercedes-Benz', 'BMW', 'Audi']; var modelArray = carArray.slice(0, 2);
Even though these arguments are optional, we predict that you will be using them to specify the starting index and the end of the collection process.
|start||Whole number used to specify initial index. Negative values can be used to select from end of an array. 0 by default.|
|end||Whole number used to specify last index for extraction (not included). If omitted, all element starting with the element specified by the start parameter are going to be selected. Negative values can be used to select from end of an array.|
Note: keep in mind that the first array element will have the index of 0, not 1.
|Return Value:||A new array which contains only the selected elements|