JavaScript Loops


I am new a this programming thing. I am now at the JavaScript course, reading assignment about loops.
I wrote this code from the book in the console, but it’s something I don’t understand.

let number = 0;
while (number <= 12) {
number = number + 2;

In the console appear all the even numbers from 0 to 14. I thought it should stop at 12.

Here is a screenshot from the console.

The 14 printed after that is not from a console.log() call, it’s the value of the last expression executed, the final number = number + 2; that brings numbers up to 14 and causes the while loop to terminate.
if you use ‘document.write(number)’ you’ll see that 14 won’t be executed.

1 Like

I understand now. Thank you very much!

1 Like