JavaScript Array slice()
Select elements:
const fruits = ["Banana","Orange","Lemon","Apple","Mango"];
const citrus = fruits.slice(1, 3);
console.log(citrus);
Select elements using negative values:
const fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
const myBest = fruits.slice(-3, -1);
console.log(myBest);
The slice() method returns selected elements in an array, as a new array.
The slice() method selects from a given start, up to a (not inclusive) given end.
The slice() method does not change the original array.
Top comments (0)