admin管理员组文章数量:1202578
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?
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?
Share Improve this question edited Jan 28, 2016 at 1:45 user4639281 asked Apr 7, 2011 at 11:54 PacerierPacerier 89.6k111 gold badges383 silver badges644 bronze badges3 Answers
Reset to default 19In 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.
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) );
本文标签: javascriptComma operator returns first value instead of second in argument listStack Overflow
版权声明:本文标题:javascript - Comma operator returns first value instead of second in argument list? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738650653a2104848.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论