为什么“警告(3> 2> 1)”警告“虚假” [英] Why does "alert(3>2>1)" alert "false"

查看:205
本文介绍了为什么“警告(3> 2> 1)”警告“虚假”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问为什么

alert(3>2>1);  // (1)

在Javascript中返回FALSE。

Is returning FALSE in Javascript.

我知道正确的是:

alert(3>2 && 2>1); // (2)

但是代码1应该返回错误消息或者为TRUE!是否有一个特定的原因,这个等式返回FALSE?

But the code 1 should return either an error message or either TRUE! Is there a specific reason that this equation returns FALSE?

推荐答案

如果你添加括号来显示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 );

这篇关于为什么“警告(3> 2> 1)”警告“虚假”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆