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?
这个怪癖有用吗?
12 个解决方案
#1
428
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)。
#2
62
My guess is because 0 < 5
is true, and true < 3
gets cast to 1 < 3
which is true.
我的猜测是,因为0 < 5是正确的,而true < 3被转换为1 < 3,这是正确的。
#3
21
probably because true
is assumed as 1
so
很可能因为假设是1。
0 < 5 < 3 --> true < 3 --> 1 < 3 --> true
#4
17
Because true < 3
, because true == 1
因为true < 3,因为true = 1
#5
10
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/ reduce作为更大表达式的一部分。您能一眼就确定这段代码的结果是什么吗?
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 guy who 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.
让你自己和下一个阅读你代码的人的生活变得轻松是一个好主意。清楚地写出你真正想要发生的事情,而不是依赖于副作用,比如布尔函数的隐式转换。
#6
9
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.
的第二部分问题的答案,“这怪癖是有用的呢?”也许是不,以前的回答如上所述,如果它确实是一个怪癖的语言(Javascript),真正的是1,但是程序员不一般视图1和真正的(0和假)同样的事情。
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为假的心理模型,那么它就会产生各种非常有用的、强大的、直接的布尔技术。例如,您可以直接使用> 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 1 0
and summing this boolean result:
并将布尔结果相加:
+/1 0 1 1 0
yields the final answer:
最后的答案:
3
3
This question is 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是否为真时。
Check if at least two out of three booleans are true
检查三种布尔值中是否至少有两种是正确的
#7
7
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?是的。让它为真,也就是1。记住这个事实,它的值是(1 < 1)1是否小于1?不,因此这是错误的。它必须是相等的。
#8
4
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'类型的操作数"
#9
4
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:
不久前,我在objc遇到了这个问题,我很困惑。我通过这样做得到了我想要的结果:
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.
这当然是假的,所以你不会得到"真"警报。很高兴看到这篇文章,现在我知道为什么了。
#10
3
In addition to python, CoffeeScript is another language that supports chained comparisons, thus 3 < x < 10
would be converted to (3 < x && x < 10)
in vanilla JS
除了python之外,CoffeeScript是另一种支持链式比较的语言,因此在vanilla JS中3 < x < 10将被转换为(3 < x && & x < 10)
#11
3
0 < 5 < 3
==> ( ( 0 < 5 ) < 3 )
==> true < 3
==> 1 < 3
==> true
#12
1
A boolean operand when operated over a math operator returns a number. to check this we do
在数学运算符上操作的布尔操作数返回一个数字。为了检查这个,我们做了
true + 1 which gives you 2.
So 0 < 5
, the returned boolean(true) operated with math operator(<) will return a number. So it boils to 1<3 which returns true
所以0 < 5,由math操作符(<)操作的返回的布尔(true)将返回一个数字。所以它可以变成1<3,返回true。
#1
428
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)。
#2
62
My guess is because 0 < 5
is true, and true < 3
gets cast to 1 < 3
which is true.
我的猜测是,因为0 < 5是正确的,而true < 3被转换为1 < 3,这是正确的。
#3
21
probably because true
is assumed as 1
so
很可能因为假设是1。
0 < 5 < 3 --> true < 3 --> 1 < 3 --> true
#4
17
Because true < 3
, because true == 1
因为true < 3,因为true = 1
#5
10
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/ reduce作为更大表达式的一部分。您能一眼就确定这段代码的结果是什么吗?
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 guy who 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.
让你自己和下一个阅读你代码的人的生活变得轻松是一个好主意。清楚地写出你真正想要发生的事情,而不是依赖于副作用,比如布尔函数的隐式转换。
#6
9
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.
的第二部分问题的答案,“这怪癖是有用的呢?”也许是不,以前的回答如上所述,如果它确实是一个怪癖的语言(Javascript),真正的是1,但是程序员不一般视图1和真正的(0和假)同样的事情。
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为假的心理模型,那么它就会产生各种非常有用的、强大的、直接的布尔技术。例如,您可以直接使用> 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 1 0
and summing this boolean result:
并将布尔结果相加:
+/1 0 1 1 0
yields the final answer:
最后的答案:
3
3
This question is 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是否为真时。
Check if at least two out of three booleans are true
检查三种布尔值中是否至少有两种是正确的
#7
7
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?是的。让它为真,也就是1。记住这个事实,它的值是(1 < 1)1是否小于1?不,因此这是错误的。它必须是相等的。
#8
4
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'类型的操作数"
#9
4
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:
不久前,我在objc遇到了这个问题,我很困惑。我通过这样做得到了我想要的结果:
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.
这当然是假的,所以你不会得到"真"警报。很高兴看到这篇文章,现在我知道为什么了。
#10
3
In addition to python, CoffeeScript is another language that supports chained comparisons, thus 3 < x < 10
would be converted to (3 < x && x < 10)
in vanilla JS
除了python之外,CoffeeScript是另一种支持链式比较的语言,因此在vanilla JS中3 < x < 10将被转换为(3 < x && & x < 10)
#11
3
0 < 5 < 3
==> ( ( 0 < 5 ) < 3 )
==> true < 3
==> 1 < 3
==> true
#12
1
A boolean operand when operated over a math operator returns a number. to check this we do
在数学运算符上操作的布尔操作数返回一个数字。为了检查这个,我们做了
true + 1 which gives you 2.
So 0 < 5
, the returned boolean(true) operated with math operator(<) will return a number. So it boils to 1<3 which returns true
所以0 < 5,由math操作符(<)操作的返回的布尔(true)将返回一个数字。所以它可以变成1<3,返回true。