If I make this line in C\C++:
int myArray[10] = { 0 }; // all elements 0
I will have an array of ten zeros.
How can I make in the same mode an array in Javascript, without for loop
and push
commands?
If I make this line in C\C++:
int myArray[10] = { 0 }; // all elements 0
I will have an array of ten zeros.
How can I make in the same mode an array in Javascript, without for loop
and push
commands?
11
undefined
elementsjoin
them by using 0
as stringsplit
the array by empty stringmap
to convert the strings to numbervar myArray = new Array(11).join('0').split('').map(Number);
console.log(myArray);
document.write('<pre>' + JSON.stringify(myArray, 0, 2) + '</pre>');
Use this
Array(10).fill(0);
Reference https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/fill#Examples