A variable declared before a loop remains available after the loop. Its lifetime is not limited to the loop block.
let count = 0;
while (count < 2) {
count += 1;
}
console.log(count); // outputs 2Predict the output
A loop counter is declared before the loop begins. Predict all messages, including the final counter value.
let step = 0;
for (; step < 3; step += 1) {
console.log("Step:", step);
}
console.log("Finished:", step);