JS 中的 if 子句 - 多个值以逗号分隔

问题描述 投票:0回答:1

为什么以下计算结果为 true?

if(false, true) {
  console.log("this is true");
}

输出为

this is true
,eslint 表示
Unexpected use of comma operator

javascript
1个回答
0
投票

通过检查,

false, true
似乎是 JavaScript 中的有效语句:

false, true;
console.log("meh");

假值包括假、零、空、空、未定义或 Nan,其他所有值(包括有效语句)均为真值。 因此,我们可以得出结论,您的

false, true
语句中的
if
条件评估为 true,这就是 print 语句发生的原因。

© www.soinside.com 2019 - 2024. All rights reserved.