How to get first N elements from an array in JavaScript

1 Answer

0 votes
const array = [3, 6, 1, 8, 9, 4, 5];

const N = 3;
const result = array.slice(0, N);

console.log(result);


  
    
    
/*
run:
    
[3, 6, 1]
    
*/

 



answered Nov 30, 2020 by avibootz

Related questions

...