Unexpected result from console.log


#1

This is my code:

var count1 = 5;
for (var i = count1; i < 20; i+=5) {
    console.log("I am counting by fives.");
};
var count2 = 2;
while (count2 < 10) {
    console.log("I am counting by twos.");
    count2 += 2;
};

Everything went as expected ("I am counting by fives." printed three times and "I am counting by twos." printed four times) , except console.log printed a "20" at the end, and I'm super confused why it did that.


#2

Are you sure that is printed 20? Not 10?

Because the javaScript console works that way - it shows you the last change you have made.
On the last line the value of count2 is changed to 10 and the console shows this to you.

If you try this:

var number = 3;
number += 2;

you will see 5 - the last change.


#3

Yes, you're right, I got a "10," not a "20," typo. Thanks very much for clarifying this!