Consider the following code snippets
var a = [1, 2, 3, 4];
for (a of a) { // The first 'a' is made by mistake
console.log(a);
}
The first a
in the for
loop is written by mistake. I think the above code should run error, because when a
is assigned to 1
in the first iteration, then a
is not iterable object. So an error should be thrown out in the next iteration.
Actually, the results are as following:
1
2
3
4
It seems the above code can iterate the array correctly. After the for
loop, the result of a
is 4
. Why?
> a
4
For further investigation, I tried to find some information from ECMA-6 doc
, but I am confused by the following statement.
for ( var ForBinding of AssignmentExpression ) Statement
for ( ForDeclaration of AssignmentExpression ) Statement
To understanding the ForBinding
and ForDeclaration
, test the following code.
var a = [1, 2, 3, 4];
for (var a of a) {
console.log(a);
}
console.log(a);
Unfortunately, the result is the same as the previous codes. What is the difference between for (var a in a)
and for (a in a)
?
Iterating over an array You can iterate over an array using for loop or forEach loop. Using the for loop − Instead on printing element by element, you can iterate the index using for loop starting from 0 to length of the array (ArrayName. length) and access elements at each index.
Using for (var property in array) will cause array to be iterated over as an object, traversing the object prototype chain and ultimately performing slower than an index-based for loop. for (... in ...) is not guaranteed to return the object properties in sequential order, as one might expect.
Iterating over an array means accessing each element of array one by one. There may be many ways of iterating over an array in Java, below are some simple ways. Method 1: Using for loop: This is the simplest of all where we just have to use a for loop where a counter variable accesses each element one by one.
The for..of loop in JavaScript allows you to iterate over iterable objects (arrays, sets, maps, strings etc).
Iterating over an array means accessing each element of array one by one. There may be many ways of iterating over an array in Java, below are some simple ways. Method 1: Using for loop: This is the simplest of all where we just have to use a for loop where a counter variable accesses each element one by one.
but this is true for iterating over any object with a for-in loop. Usually the order of the items in an array is important, but the for-in loop won't necessarily iterate in the right order, that's because it treats the array as an object, which is the way it is implemented in JS, and not as an array.
Since JavaScript elements are saved as standard object properties, it is not advisable to iterate through JavaScript arrays using for...in loops because normal elements and all enumerable properties will be listed.
The array is a homogeneous collection of data which you can iterate and print each element using the loop. To iterate each element and print, you need to use condition variable less than the array length as given below example.
for
evaluates value of "AssignmentExpression" and iterates over it. The value is obtained only once in the beginning of iteration, so reusing the same variable is perfectly valid (also quite confusing).
Presence of var
: for (a of ...)
and for (var a of ...)
does not make any 1 difference in code as you already have a
defined - so it will just re-declare the same variable.
To be completely precise there are cases where behavior is different - when a
is declared in outer scope for current function var
version will shadow that value (as in JavaScript all var
statements are hoisted to top of the function scope):
var a = [1,2,3];
function tryForVar() {
// Note that declaration of `a` is hoisted here as var a = undefined;
// for (var a ... does not work as expected as local 'a' is undefined
for (var a of a) {
console.log(a); // log 'undefined' once
}
console.log(a); // undefined
}
tryForVar();
console.log(a); // [1,2,3]
function tryFor() {
// Note that declaration of `a` from outer scope
// for (a ... works fine as it uses outer 'a'
for (a of a) {
console.log(a); // logs all 1,2,3 in sequence
}
console.log(a); // 3
}
tryFor();
console.log(a); // 3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With