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

前端 未结 3 996
余生分开走
余生分开走 2020-12-14 12:31

MDN claims that:

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

3条回答
  •  隐瞒了意图╮
    2020-12-14 13:03

    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.

提交回复
热议问题