One short way of doing it would be:
var arr = Array(arraySize).fill(value);
Would make arr = Array [ 0, 0, 0, 0, 0 ] if arraySize == 5 and value == 0, for example.
One short way of doing it would be:
var arr = Array(arraySize).fill(value);
Would make arr = Array [ 0, 0, 0, 0, 0 ] if arraySize == 5 and value == 0, for example.
while(arraySize--) array.push(value);
no initialization (that i know of)
Update
Since ever posting this answer 4 years ago, people seem to keep coming back here for this answer. For benchmarking purposes I made a JSPerf with some different solutions.
The solution above here isn't the quickest, although it's short. To stick to the same short style, but with a better performance:
while(size--) array[size] = value;
Update Feb 2016 Updated the JSPerf with a new revision with more testcases.
If performance doesn't matter and you want a one-liner:
var value = 1234, // can be replaced by a fixed value
size = 1000, // can be replaced by a fixed value
array = Array.apply(null,{length: size}).map(function() { return value; });
A more performant solution (in one, dirty, line): Be aware: this replaces existsing value, size and i variables in the scope
for(var i = 0, value = 1234, size = 1000, array = new Array(1000); i < size; i++) array[i] = value;
In your first example, you are making a blank array, same as doing var x = []. The 2nd example makes an array of size 3 (with all elements undefined). The 3rd and 4th examples are the same, they both make arrays with those elements.
Be careful when using new Array().
var x = new Array(10); // array of size 10, all elements undefined
var y = new Array(10, 5); // array of size 2: [10, 5]
The preferred way is using the [] syntax.
var x = []; // array of size 0
var y = [10] // array of size 1: [1]
var z = []; // array of size 0
z[2] = 12; // z is now size 3: [undefined, undefined, 12]
The preferred way is to always use the literal syntax with square brackets; its behaviour is predictable for any number of items, unlike Array's. What's more, Array is not a keyword, and although it is not a realistic situation, someone could easily overwrite it:
function Array() { return []; }
alert(Array(1, 2, 3)); // An empty alert box
However, the larger issue is that of consistency. Someone refactoring code could come across this function:
function fetchValue(n) {
var arr = new Array(1, 2, 3);
return arr[n];
}
As it turns out, only fetchValue(0) is ever needed, so the programmer drops the other elements and breaks the code, because it now returns undefined:
var arr = new Array(1);
Array(5)gives you an array with length 5 but no values, hence you can't iterate over it.Array.apply(null, Array(5)).map(function () {})gives you an array with length 5 and undefined as values, now it can be iterated over.Array.apply(null, Array(5)).map(function (x, i) { return i; })gives you an array with length 5 and values 0,1,2,3,4.Array(5).forEach(alert)does nothing,Array.apply(null, Array(5)).forEach(alert)gives you 5 alertsES6gives usArray.fromso now you can also useArray.from(Array(5)).forEach(alert)If you want to initialize with a certain value, these are good to knows...
Array.from('abcde'),Array.from('x'.repeat(5))
orArray.from({length: 5}, (v, i) => i) // gives [0, 1, 2, 3, 4]
With ES2015 .fill() you can now simply do:
// `n` is the size you want to initialize your array
// `0` is what the array will be filled with (can be any other value)
Array(n).fill(0)
Which is a lot more concise than Array.apply(0, new Array(n)).map(i => value)
It is possible to drop the 0 in .fill() and run without arguments, which will fill the array with undefined. (However, this will fail in Typescript)