for of loop example
Example 1: for of loop javascript
const array1 = ['a', 'b', 'c'];
for (const element of array1) {
console.log(element);
}
Example 2: for?of loop
const iterable = [10, 20, 30];
for (const value of iterable) {
console.log(value);
}
Example 3: Iterate with JavaScript For Loops
var myArray = [];
for (var i = 1; i <= 5; i++){
myArray.push(i);
}
console.log(myArray)
Example 4: mdn for..of
function* fibonacci() {
let [prev, curr] = [0, 1];
while (true) {
[prev, curr] = [curr, prev + curr];
yield curr;
}
}
for (const n of fibonacci()) {
console.log(n);
if (n >= 1000) {
break;
}
}