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

2 Answers

0 votes
const N = 6;

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

 



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

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

 



answered Jul 7, 2022 by avibootz
...