DrStrangeLove
DrStrangeLove

Reputation: 11557

Javascript expression in parentheses

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

Answers (2)

Ned Batchelder
Ned Batchelder

Reputation: 375604

Javascript has a comma operator, like C does. It evaluates each of the expressions, then returns the last one.

Upvotes: 13

cHao
cHao

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

Related Questions