I use this syntax often but I don't understand why it simply works.
var a = 1, b = a + 1;
console.log(b); // 2
In the event you're declaring a var, after splitting them on a comma, the b
already sees a
as evaluated? Why is that?
It has been defined as such.
First, the definition of the essential terms:
VariableStatement :
var
VariableDeclarationList;
and:
VariableDeclarationList :
VariableDeclaration
VariableDeclarationList , VariableDeclaration
The actual definition which applies to your question is this one:
The production VariableDeclarationList : VariableDeclarationList , VariableDeclaration is evaluated as follows:
Evaluate VariableDeclarationList.
Evaluate VariableDeclaration.
So at the time #2 is executed, #1 has been evaluated already and you can use any effect of it in #2.