Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

how to create a slice of the array with n elements taken from the beginning in javascript

// how to create a slice of the array with n elements taken from the beginning in javascript
const take = (arr, n) => arr.slice(0, n);
console.log(take([1, 2, 3], 2)); // [1, 2]
console.log(take([1, 2, 3], 0)); // []
 
PREVIOUS NEXT
Tagged: #create #slice #array #elements #beginning #javascript
ADD COMMENT
Topic
Name
4+7 =