For starters there is no point is using a for in loop on an Array because arrays in JavaScript can only have numeric ordered, numeric indexes. So you can access the array at any index between the range of 0 and array.length - 1 Alas, if you wanted to use a for in loop to iterate over an array you certainly can, however, a regular for loop is more appropriate.
A for in loop is used when you don't have ordered numeric indices. JavaScript objects are really an ordered hash table. You can access keys of JavaScript objects with the in operator which returns the key for the object, and by accessing the object at that key you can get the value.
For example:
var obj = {
hello: "world",
world: "hello"
};
A regular for loop wouldn't work, so you would need t use a for in loop.
for(var i in obj) console.log(obj[i]);
Objects also don't return a length property that is accurate enough to iterate over the entire object, so a for in loop is absolutely necessary.
Also note that by assigning values to the array not in the order in which the next free element would exist will automatically place undefined in the elements that you skipped.
In your example, the array would look like this:
[0, 1, undefined ×ばつ 2, 4]
For starters there is no point is using a for in loop on an Array because arrays in JavaScript can only have numeric ordered, numeric indexes. So you can access the array at any index between the range of 0 and array.length - 1 Alas, if you wanted to use a for in loop to iterate over an array you certainly can, however, a regular for loop is more appropriate.
A for in loop is used when you don't have ordered numeric indices. JavaScript objects are really an ordered hash table. You can access keys of JavaScript objects with the in operator which returns the key for the object, and by accessing the object at that key you can get the value.
For example:
var obj = {
hello: "world",
world: "hello"
};
A regular for loop wouldn't work, so you would need t use a for in loop.
for(var i in obj) console.log(obj[i]);
Objects also don't return a length property that is accurate enough to iterate over the entire object, so a for in loop is absolutely necessary.
For starters there is no point is using a for in loop on an Array because arrays in JavaScript can only have ordered, numeric indexes. So you can access the array at any index between the range of 0 and array.length - 1 Alas, if you wanted to use a for in loop to iterate over an array you certainly can, however, a regular for loop is more appropriate.
A for in loop is used when you don't have ordered numeric indices. JavaScript objects are really an ordered hash table. You can access keys of JavaScript objects with the in operator which returns the key for the object, and by accessing the object at that key you can get the value.
For example:
var obj = {
hello: "world",
world: "hello"
};
A regular for loop wouldn't work, so you would need t use a for in loop.
for(var i in obj) console.log(obj[i]);
Objects also don't return a length property that is accurate enough to iterate over the entire object, so a for in loop is absolutely necessary.
Also note that by assigning values to the array not in the order in which the next free element would exist will automatically place undefined in the elements that you skipped.
In your example, the array would look like this:
[0, 1, undefined ×ばつ 2, 4]
For starters there is no point is using a for in loop on an Array because arrays in JavaScript can only have numeric ordered, numeric indexes. So you can access the array at any index between the range of 0 and array.length - 1 Alas, if you wanted to use a for in loop to iterate over an array you certainly can, however, a regular for loop is more appropriate.
A for in loop is used when you don't have ordered numeric indices. JavaScript objects are really an ordered hash table. You can access keys of JavaScript objects with the in operator which returns the key for the object, and by accessing the object at that key you can get the value.
For example:
var obj = {
hello: "world",
world: "hello"
};
A regular for loop wouldn't work, so you would need t use a for in loop.
for(var i in obj) console.log(obj[i]);
Objects also don't return a length property that is accurate enough to iterate over the entire object, so a for in loop is absolutely necessary.