javascript for loop let of code example
Example 1: javascript for of
const numbers = [1,2,3,4];
for(const item of numbers){
console.log(item);
}
Example 2: mdn for..of
function* fibonacci() { // a generator function
let [prev, curr] = [0, 1];
while (true) {
[prev, curr] = [curr, prev + curr];
yield curr;
}
}
for (const n of fibonacci()) {
console.log(n);
// truncate the sequence at 1000
if (n >= 1000) {
break;
}
}