How to set all array elements to a specific value in JavaScript

2 Answers

0 votes
const arr = new Array(4).fill('javascript');

console.log(arr);
  
  
  
  
/*
run:
  
["javascript", "javascript", "javascript", "javascript"]
  
*/

 



answered May 13, 2022 by avibootz
0 votes
let arr = [5, 8, 1, 9, 0];

arr = new Array(arr.length).fill(30);

console.log(arr);
  
  
  
  
/*
run:
  
[30, 30, 30, 30, 30]
  
*/

 



answered May 13, 2022 by avibootz
...