Code
var cool = new Array(3);
cool[setAll] = 42; //cool[setAll] is just a pseudo selector..
alert(cool);
Result
Actually, you can use this perfect approach:
let arr = Array.apply(null, Array(5)).map(() => 0);
// [0, 0, 0, 0, 0]
This code will create array and fill it with zeroes. Or just:
let arr = new Array(5).fill(0)
The other answers are Ok, but a while loop seems more appropriate:
function setAll(array, value) {
var i = array.length;
while (i--) {
array[i] = value;
}
}
A more creative version:
function replaceAll(array, value) {
var re = new RegExp(value, 'g');
return new Array(++array.length).toString().replace(/,/g, value).match(re);
}
May not work everywhere though. :-)