gpt4 book ai didi

javascript - JavaScript 中逗号运算符的作用是什么?

转载 作者:行者123 更新时间:2023-11-28 04:57:07 25 4
gpt4 key购买 nike

如果我使用:

1.09 * 1; // returns "1.09"

但是如果我使用:

1,09 * 1; // returns "9"

我知道 1,09 不是一个数字。

最后一段代码中的逗号有什么作用?

更多示例

if (0,9) alert("ok"); // alert
if (9,0) alert("ok"); // don't alert
<小时/>
alert(1); alert(2); alert(3); // 3 alerts
alert(1), alert(2), alert(3); // 3 alerts too
<小时/>
alert("2",
foo = function (param) {
alert(param)
},
foo('1')
)
foo('3'); // alerts 1, 2 and 3

最佳答案

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

来源: https://developer.mozilla.org/en/JavaScript/Reference/Operators/Special_Operators/Comma_Operator

例如,表达式 1,2,3,4,5 的计算结果为 5。显然,逗号运算符仅对有副作用的操作有用。

console.log(1,2,3,4,5);
console.log((1,2,3,4,5));

关于javascript - JavaScript 中逗号运算符的作用是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42452244/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com