JS - Why does this code iterate 3 times? [solved]

Hello, I’m having a hard time understanding why the code:

for (let i = 0; i < 99; i++) {
if (i > 2 ) {
break;
}
console.log(‘Banana.’);
}

console.log(‘Orange you glad I broke out the loop!’);

outputs ‘banana’ 3 times.

Count the iterations before the break…

0
1
2
break

Each iteration prints ‘banana’, and there are only three iterations regardless the upper limit on the loop.

1 Like

Thank you! I was thinking it would print banana for each instance that i > 2, but now I see that is what triggers the break. I was thinking we “broke” out 3 times, and was wondering why it didn’t do it when i = 3 through 98…

1 Like