Reputation: 11557
var x = (1,2,3);
alert(x);
This expression evaluates to 3.
How is this expression (1,2,3)
called? Why does it return 3?
Upvotes: 9
Views: 456
Reputation: 375604
Javascript has a comma operator, like C does. It evaluates each of the expressions, then returns the last one.
Upvotes: 13
Reputation: 86525
I haven't seen this in Javascript before. But in a number of other C'ish languages, it basically evaluates each of the expressions in the parentheses and returns the value of the last one.
Upvotes: 2