I don't understand the simple JavaScript 'while loop'

I am currently learning JavaScript at Code Academy , and I am at the end of the while while loop.

The following code:

j = 0;
while (j < 3) {
    j++;
}

creates 2on the console, and I don't know why. I tried to run this on Eclipse JaveEEjust to understand that using an HTML file with this code as a script gives me a different result: a blank page.

This makes sense to me because I only increased jto 3, but did not print it out. Not sure why CodeAcademy console gives me 2.

This is a screenshot of the console output:

codeacademy console output

+4
source share
1 answer

, , , .

, , . - 2 + 2 4.

, , , . , , , , , .

, :

j = 0;
while (j < 3) {
    j++;
}

, j++;. 2, j . postfix - 2.

j = 0;
while (j < 3) {
    ++j;
}

3.

- :

j = 0;
while (j < 3) {
    j++;
    a = 42;
}

42. a = 42 .

j = 0;
while (j < 3) {
    j++;
    var a = 42;
}

2, .

: , -, , . , console.log() , .

+6

All Articles