How to extract the first 2 elements of an array in Javascript

1 Answer

0 votes
const arr = ['javascript', 'typescript', 'node.js', 'c++'];

const first2 = arr.slice(0, 2);

console.log(first2); 

  
  
  
  
/*
run:
  
["javascript", "typescript"]
  
*/

 



answered Jun 10, 2022 by avibootz
...