+ 3

What is the useful of for...of in javascript

2nd Sep 2019, 7:19 PM
Chamseddine Louhmadi
Chamseddine Louhmadi - avatar
3 Réponses
+ 3
Consider these two codes: // Code 1 var arr = ["a", "b", 1, 2]; for (var i = 0; i < arr.length; i++) { console.log(arr[i]); } // Code 2 var arr = ["a", "b", 1, 2]; for (var elem of arr) { console.log(elem); } Both of these codes have the same output, but it is slightly easier to read the second one. For/of loops are especially useful if you have an array of objects and you are accessing their values or methods.
3rd Sep 2019, 2:46 AM
Daniel C
Daniel C - avatar
+ 2
In a for of loop it is easier to get all elements of an array without the use of i or some counter
2nd Sep 2019, 8:33 PM
Nico Ruder
Nico Ruder - avatar