Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comma operator returns first value instead of second in argument list?

MDN claims that:

The comma operator evaluates both of its operands (from left to right) and returns the value of the second operand.

However, when I tried running <script> alert(1, 2); </script>, it shows a "1" instead of a "2".

Am I misunderstanding something?

like image 745
Pacerier Avatar asked Apr 07 '11 11:04

Pacerier


1 Answers

In the context of a function call, the comma is used to separate parameters from each other. So what you're doing is passing a second parameter to alert() which gets silently ignored.

What you want is possible this way:

 alert((1,2));

The extra brackets form a parameter on their own; inside them you can use the comma as an operator.

like image 125
Pekka Avatar answered Nov 04 '22 10:11

Pekka