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

↘锁芯ラ 提交于 2019-11-30 03:57:07

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.

MadBender

Comma(,) is also a parameter separator.

Use alert((1,2)) instead.

When you use it like that, the comma is not an operator, it's a separator between the parameters in the call to the alert method.

If you put parentheses around them so that it's an expression, it will show you 2:

alert( (1,2) );
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!