且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么“警告(3> 2> 1)”警告“虚假”

更新时间:2022-04-18 04:47:11

如果你添加括号来显示JavaScript如何解释它,它会得到更清楚:

If you add parentheses to show how JavaScript is interpreting it, it gets much clearer:

alert( (3 > 2) > 1 );

让我们分开。首先,它评估 3> 2 。是的,三个大于两个。因此,您现在拥有:

Let's pick this apart. First, it evaluates 3 > 2. Yes, three is greater than two. Therefore, you now have this:

alert( true > 1 );

true 被强制转换为数字。这个数字恰好是 1 1> 1 显然是假的。因此,结果是:

true is coerced into a number. That number happens to be 1. 1 > 1 is obviously false. Therefore, the result is:

alert( false );