First, I'd like to note that such a loop is considered bad style, since it's very unreadable and causes a lot of confusion. This is a typical example of optimization gone wrong.
Looking at the specs, you'll find that for(...)
must be followed by a statement.
It can be any statement, including blocks. So all of these are valid:
for (...)
foo; // expression statement
,
for(...)
{
// block statement
}
,
for(...)
if(...) // If statement
foo;
, and of course
for (...)
;
since ";
" is the empty statement. It does nothing, but it's enough to make for(...);
syntactically valid.
Now, for the commas. Note that the contents of the parens must be three expressions, (each of them optional), separated by semicolons. Pretty much "everything" qualifies as expressions, including comma-separated lists of expressions. Though little known, these work basically everywhere in JS, not only in for
loops. They are simply evaluated one after the other.
So, your loop can be rewritten like so
shuffle = function(o) {
var j, x, i = o.length;
while (i) { // for-loops are just while-loops in disguise
j = parseInt(Math.random() * i), // even better: replace , by ;
x = o[--i],
o[i] = o[j],
o[j] = x;
}
return o;
};
Also, x = o[--i]
should be written as i--; x = o[i]
.