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:
xxxxxxxxxx
1
const array = ['a', 'b', 'c', 'd'];
2
const n = 3;
3
4
const slicedElements = array.slice(0, n);
5
6
console.log(slicedElements);
Note:
The
slice()
method returns a shallow copy of the original array and doesn't modify it.