Q:

How does a for-loop with four 'things' in the parenthesis work i Javascript?

I came across some code here https://stackoverflow.com/a/2922778/2176900 that started off like this:
for (i = 0, j = nvert-1; i < nvert; j = i++) {...`

and I don't understand what it does when it has four things within the parenthesis ("i = 0", "j = nvert-1", "i < nvert" and "j = i++"). I've obviously been trying to google this, but can't seem to find any answers. I'm sure if I just knew what the things in the parenthesis was called I'd be glad.
Thanks

A:

It doesn't have four things; it still has three.

The pre-loop statement
i = 0, j = nvert-1
The iteration condition
i < nvert
The post-iteration action
j = i++

That pre-loop statement is actually probably supposed to read:
var i = 0, j = nvert-1
Remember that we can declare multiple variables in a single declaration. That's what's happening here.

A:

To add to the excellent answers from @LightnessRacesinOrbit and @basilikum, here is a tip I find helpful when looking at an unusual for loop like this: break it down to a more primitive form.
Any for loop:
for( initialize; condition; advance ) {
    // loop body here
}

can be translated to an equivalent while loop:
initialize;
while( condition ) {
    // loop body here
    advance;
}

So the loop you found:
for ( i = 0, j = nvert-1;  i < nvert;  j = i++ ) {
    // loop body here
}

could be written:
i = 0,  j = nvert - 1;
while( i < nvert ) {
    // loop body here
    j = i++;
}

Now we can take that and break it down to even simpler steps.
As @basilikum noted, the first line:
i = 0,  j = nvert - 1;

is the same as:
i = 0;
j = nvert - 1;

and the last line of the loop:
    j = i++;

is the same as:
    j = i;
    i = i + 1;

So putting those back into the code we end up with:
i = 0;
j = nvert - 1;
while( i < nvert ) {
    // loop body here
    j = i;
    i = i + 1;
}

That's more verbose than the original for loop, but it may be easier to think about if the original loop is confusing.

