+ 1
What is the difference between for in and For of loops?
When to use for in, for of loops and what is the relation to for loop.
8 Answers
+ 11
Pablo Escobar ll
One more example, where use for..of Loop!;)đ
https://code.sololearn.com/WxEg6de0NtKy/?ref=app
+ 8
for ⊠of Loops
ES6 adds a way to iterate over each of the values in an array. This is different from the existing for ... in loop that loops over the key/index.
let a = ['a', 'b', 'c', 'd' ];
// ES6
for ( var val of a ) {
console.log( val );
} // "a" "b" "c" "d"
// pre-ES6
for ( var idx in a ) {
console.log( idx );
} // 0 1 2 3
Using the new for ... of loop saves adding a let val = a[idx] inside each loop.
https://www.sololearn.com/learn/JavaScript/2970/
+ 8
Pablo Escobar ll Check out my Code, it's edited!
for..in loops over the keys/indexes in the array, while for..of loops over the values in array.
The value you loop over with for..of must be a value which can be coerced/boxed to an object!
+ 4
Hope this helps https://code.sololearn.com/WIpjWv8FZfJ7/?ref=app
+ 2
Danijel IvanoviÄ
So for..in used for objects and for..Of for arrays.
+ 1
Danijel lvanovic still confusing
0
D'lite that's okay