The slice method

The slice() method is used to slice a specific section of an array then to create a new array using the elements from the sliced section of the old array.

var Fruits = new Array ("pears", "mangoes" , "bananas" , "apples");
var FewFruits = Fruits.slice(1,3);
document.write(FewFruits);

Initially, an array that contains four elements is created.
On the second line of code, using slice() method, the second and the third element are eliminated (index 1 eliminates the second element in the array and index 3 represents the first element that won’t be eliminated). The last line of code writes on the document the elements of the new array.

The output will be “mangoes,bananas”.

Leave a Reply