+ 3
What is the useful of for...of in javascript
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.
+ 2
In a for of loop it is easier to get all elements of an array without the use of i or some counter