How to create an array with N elements and same values in JavaScript

2 Answers

0 votes
const N = 5;

const arr = Array(N).fill(1);
     
console.log(arr);
 

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

 



answered Jul 7, 2022 by avibootz
0 votes
const N = 5;

const arr = Array(N).fill('*');
     
console.log(arr);
 
 
 
 
/*
run:
 
["*", "*", "*", "*", "*"]
 
*/
  

 



answered Jul 7, 2022 by avibootz
...