Your code fails with a ReferenceError
because key1
, key2
etc don't exist - the JavaScript runtime doesn't know what value you are asking for.
Your example doesn't actually need these variables to be set. If you simply want an Array of 35 elements, all set to 0
, you can use:
var keys = [];
for(var i = 0; i < 36; i ++) {
keys.push(0);
}
For other ways to do this, see Most efficient way to create a zero filled JavaScript array?
Looking a small distance into the future, ES6 provides Array.prototype.fill
:
var keys = new Array(35);
keys.fill(0);
This method is not widely implemented yet, but you can try it out in Firefox 31+.
ES6 compatibility table