DEV Community

Tsito Ranjafy
Tsito Ranjafy

Posted on

Select Element in Array() to a new Array() JavaScript

JavaScript Array slice()
Select elements:

const fruits = ["Banana","Orange","Lemon","Apple","Mango"];
const citrus = fruits.slice(1, 3);
console.log(citrus);
Enter fullscreen mode Exit fullscreen mode

Select elements using negative values:

const fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
const myBest = fruits.slice(-3, -1);
console.log(myBest);
Enter fullscreen mode Exit fullscreen mode

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)