javascript 为什么 (0 < 5 < 3) 返回 true?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4089284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does (0 < 5 < 3) return true?
提问by punkrockbuddyholly
I was playing around in jsfiddle.net and I'm curious as to why this returns true?
我在 jsfiddle.net 上玩,我很好奇为什么这会返回 true?
if(0 < 5 < 3) {
alert("True");
}
So does this:
这样做也是如此:
if(0 < 5 < 2) {
alert("True");
}
But this doesn't:
但这不会:
if(0 < 5 < 1) {
alert("True");
}
Is this quirk ever useful?
这个怪癖有用吗?
回答by Alan Geleynse
Order of operations causes (0 < 5 < 3)to be interpreted in javascript as ((0 < 5) < 3)which produces (true < 3)and true is counted as 1, causing it to return true.
操作顺序导致(0 < 5 < 3)在 javascript 中被解释为((0 < 5) < 3)哪个产生(true < 3)并且 true 被计为 1,导致它返回 true。
This is also why (0 < 5 < 1)returns false, (0 < 5)returns true, which is interpreted as 1, resulting in (1 < 1).
这也是为什么(0 < 5 < 1)返回 false,(0 < 5)返回 true,这被解释为1,导致(1 < 1)。
回答by CaffGeek
My guess is because 0 < 5is true, and true < 3gets cast to 1 < 3which is true.
我的猜测是因为0 < 5是真的,true < 3并被强制转换1 < 3为真。
回答by Hyman
probably because trueis assumed as 1so
可能是因为true假设1如此
0 < 5 < 3 --> true < 3 --> 1 < 3 --> true
回答by Harmen
Because true < 3, because true == 1
因为true < 3,因为true == 1
回答by Zach Johnson
As to your question whether this quirk is ever useful: I suppose there could be some case where it would useful (if condensed code is what you are after), but relying on it will (most likely) severely reduce the understandability of your code.
至于你这个怪癖是否有用的问题:我想在某些情况下它可能有用(如果你想要的是浓缩代码),但依赖它(很可能)会严重降低你的代码的可理解性。
It's kind of like using post/pre increment/decrement as a part of bigger expressions. Can you determine what this code's result is at a glance?
这有点像使用 post/pre increment/decrement 作为更大表达式的一部分。你能一眼就判断出这段代码的结果是什么吗?
int x = 5;
int result = ++x + x++ + --x;
Note: with this code, you can sometimes even get different results depending on the language and compiler.
注意:使用此代码,有时甚至可以根据语言和编译器获得不同的结果。
It's a good idea to make life easy for yourself and the next guywho will read your code. Clearly write out what you actually want to have happen rather then relying on side effects like the implicit conversion of booleans.
让你自己和下一个阅读你的代码的人的生活变得轻松是个好主意。清楚地写出您真正想要发生的事情,而不是依赖于诸如布尔值的隐式转换之类的副作用。
回答by PAUL Mansour
The answer to the second part of the question, "is this quirk ever useful?" is perhaps no, as noted by a previous answer, if it is indeed a quirk of the language (Javascript) that true is cast to 1, but that the programmer does not in general view 1 and true (and 0 and false) as the same thing.
问题第二部分的答案,“这个怪癖有用吗?” 也许不是,正如之前的答案所指出的那样,如果 true 被强制转换为 1 确实是语言 (Javascript) 的一个怪癖,但是程序员通常不会将 1 和 true(以及 0 和 false)视为一样。
If however you have a mental model of 1 being true and 0 being false, then it leads to all sorts of nice boolean techniques that are extremely useful, powerful, and direct. For example, you could increment a counter directly with the result of A > 100, which would increment the counter if A is greater than 100. This technique might be viewed as a quirk or a trick in Java, but in an array or functional language may be idiomatic.
然而,如果你有一个 1 为真而 0 为假的心智模型,那么它会导致各种非常有用、强大和直接的很好的布尔技术。例如,您可以直接使用 A > 100 的结果递增计数器,如果 A 大于 100,则计数器递增。此技术在 Java 中可能被视为怪癖或技巧,但在数组或函数式语言中可能是惯用的。
A classic example in the array language APL would be to count the number of items in an array that are (say) greater than 100:
数组语言 APL 中的一个经典示例是计算数组中(例如)大于 100 的项目数:
+/A>100
Where if A is the 5 item array 107 22 256 110 3 then:
如果 A 是 5 项数组 107 22 256 110 3 那么:
A>100
yields the 5 item boolean array:
产生 5 项布尔数组:
1 0 1 1 0
1 0 1 1 0
and summing this boolean result:
并总结这个布尔结果:
+/1 0 1 1 0
yields the final answer:
得出最终答案:
3
3
This questionis a perfect example of where this technique would be very useful, especially if the problem is generalized to determine if n out of m boolean values are true.
这个问题是这个技术非常有用的一个完美例子,特别是如果问题被概括为确定 m 个布尔值中的 n 个是否为真。
回答by netrox
That's easy.
这很容易。
(0 < 5 < 3)
Start with left to right so it evaluates the first 0 < 5. Is it true? Yes. Since TRUE=1, it evaluates 1 < 3. Since 1 is less than 3 so it's true.
从左到右开始,所以它评估第一个 0 < 5。这是真的吗?是的。由于 TRUE=1,它计算 1 < 3。因为 1 小于 3,所以它是真的。
Now with this
现在有了这个
(0 < 5 < 1)
Is 0 less than 5? Yes. So make it TRUE which also means 1. Now with that fact in mind, it evaluates to (1 < 1). Is 1 less than 1? No, therefore it's false. It has to be equal.
0 小于 5 吗?是的。所以让它为 TRUE,这也意味着 1。现在考虑到这个事实,它的计算结果为 (1 < 1)。1 小于 1 吗?不,所以它是假的。它必须是平等的。
回答by David
is it evaluating 0<5 which would return 1 for true when 1<3 which is true?
是否正在评估 0<5,当 1<3 为真时,它会返回 1 为真?
C# want let you do this "Operator '<' cannot be applied to operands of type 'bool' and 'int'"
C#想让你这样做“运算符'<'不能应用于'bool'和'int'类型的操作数”
回答by Hippocrates
I ran into this a little while ago in Obj-C and was very puzzled by it. I got the results I wanted by doing something like this:
不久前我在 Obj-C 中遇到了这个问题,并对此感到非常困惑。我通过做这样的事情得到了我想要的结果:
if(0 < 5 && 5 < 3) {
alert("True");}
Which of course is false so you wouldn't get that "true" alert. Glad I read this, I now know why.
这当然是错误的,所以你不会得到那个“真实”的警报。很高兴我读到了这个,我现在知道为什么了。
回答by Hippocrates
In addition to python, CoffeeScript is another language that supports chained comparisons, thus 3 < x < 10would be converted to (3 < x && x < 10)in vanilla JS
除了 python,CoffeeScript 是另一种支持链式比较的语言,因此3 < x < 10会(3 < x && x < 10)在 vanilla JS 中转换为

