EN
JavaScript - get first n number of elements from array
0
points
In this article, we would like to show you how to get first n number of elements from an array in JavaScript.
Quick solution:
// ONLINE-RUNNER:browser;
const array = ['a', 'b', 'c', 'd'];
const n = 3;
const slicedElements = array.slice(0, n);
console.log(slicedElements);
Note:
The
slice()method returns a shallow copy of the original array and doesn't modify it.