+ 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.

26th Nov 2018, 9:09 AM
UwU Discover Me
8 Respuestas
+ 11
Pablo Escobar ll One more example, where use for..of Loop!;)👍 https://code.sololearn.com/WxEg6de0NtKy/?ref=app
26th Nov 2018, 5:09 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 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/
26th Nov 2018, 9:41 AM
Danijel Ivanović
Danijel Ivanović - avatar
+ 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!
26th Nov 2018, 12:06 PM
Danijel Ivanović
Danijel Ivanović - avatar
26th Nov 2018, 10:30 AM
Dlite
Dlite - avatar
+ 2
Danijel Ivanović So for..in used for objects and for..Of for arrays.
26th Nov 2018, 11:36 AM
UwU Discover Me
+ 1
Danijel lvanovic still confusing
26th Nov 2018, 10:04 AM
UwU Discover Me
0
D'lite that's okay
26th Nov 2018, 10:40 AM
UwU Discover Me