javascript iterative statements code example
Example 1: js for loops
let i;
for (i = 0; i < 6; ++i) {
console.log("Hello"); // print Hello 6 times
}
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;
}
}