JavaScript 中的 true == 1 和 false == 0 是真的吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12236271/
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
Is true == 1 and false == 0 in JavaScript?
提问by Mahesha999
I was reading a good book on JavaScript.
我正在阅读一本关于 JavaScript 的好书。
It started with:
它始于:
Boolean type take only two literal values: true and false. These are distinct from numeric values, so true is not equal to 1, and false is not equal to 0.
布尔类型仅采用两个字面值:true 和 false。它们与数值不同,因此 true 不等于 1,false 不等于 0。
However, I observed following:
但是,我观察到以下情况:
if(1==true)
document.write("oh!!! that's true"); //**this is displayed**
I know, that every type in JavaScript has a Boolean equivalent.
我知道,JavaScript 中的每种类型都有一个 Boolean 等价物。
But then, what's the truth?
但是,真相是什么?
回答by Guffa
It's true that true
and false
don't represent any numerical values in Javascript.
确实,true
并且false
不代表 Javascript 中的任何数值。
In some languages (e.g. C, VB), the boolean values are defined as actual numerical values, so they are just different names for 1 and 0 (or -1 and 0).
在某些语言(例如 C、VB)中,布尔值被定义为实际数值,因此它们只是 1 和 0(或 -1 和 0)的不同名称。
In some other languages (e.g. Pascal, C#), there is a distinct boolean type that is not numerical. It's possible to convert between boolean values and numerical values, but it doesn't happen automatically.
在其他一些语言(例如 Pascal、C#)中,有一个不同的布尔类型不是数字。可以在布尔值和数值之间进行转换,但它不会自动发生。
Javascript falls in the category that has a distinct boolean type, but on the other hand Javascript is quite keen to convert values between different data types.
Javascript 属于具有独特布尔类型的类别,但另一方面,Javascript 非常热衷于在不同数据类型之间转换值。
For example, eventhough a number is not a boolean, you can use a numeric value where a boolean value is expected. Using if (1) {...}
works just as well as if (true) {...}
.
例如,即使数字不是布尔值,您也可以在需要布尔值的地方使用数字值。使用if (1) {...}
与if (true) {...}
.
When comparing values, like in your example, there is a difference between the ==
operator and the ===
operator. The ==
equality operator happily converts between types to find a match, so 1 == true
evaluates to true because true
is converted to 1
. The ===
type equality operator doesn't do type conversions, so 1 === true
evaluates to false because the values are of different types.
在比较值时,就像在您的示例中一样,==
运算符和===
运算符之间存在差异。在==
平等的经营者类型之间的转换高高兴兴地找到匹配,因此1 == true
计算结果为真实的,因为true
转换为1
。该 ===
类型相等运算符不进行类型转换,因此1 === true
计算结果为假,因为值是不同的类型。
回答by Madara's Ghost
In JavaScript, == is pronounced "Probably Equals".
在 JavaScript 中,== 发音为“可能等于”。
What I mean by that is that JavaScript will automatically convert the Boolean into an integer and then attempt to compare the two sides.
我的意思是 JavaScript 会自动将布尔值转换为整数,然后尝试比较两侧。
For real equality, use the === operator.
对于真正的相等,请使用 === 运算符。
回答by Evandro Silva
Try the strict equality comparison:
尝试严格相等比较:
if(1 === true)
document.write("oh!!! that's true"); //**this is not displayed**
The ==
operator does conversion from one type to another, the ===
operator doesn't.
该==
运营商不转换,从一种类型到另一个时,===
运营商没有。
回答by Jo?o Silva
From the ECMAScript specification, Section 11.9.3 The Abstract Equality Comparison Algorithm:
来自 ECMAScript 规范的第11.9.3节抽象平等比较算法:
The comparison x == y, where x and y are values, produces true or false. Such a comparison is performed as follows:
- If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
比较 x == y,其中 x 和 y 是值,产生真或假。这样的比较如下:
- 如果 Type(y) 是布尔值,则返回比较结果x == ToNumber(y)。
Thus, in, if (1 == true)
, true
gets coerced to a Number
, i.e. Number(true)
, which results in the value of 1
, yielding the final if (1 == 1)
which is true
.
因此,在,if (1 == true)
,true
被强制转换为Number
,即Number(true)
,其结果的值1
,得到最终的if (1 == 1)
是true
。
if (0 == false)
is the exact same logic, since Number(false) == 0
.
if (0 == false)
是完全相同的逻辑,因为Number(false) == 0
.
This doesn't happen when you use the strict equals operator ===
instead:
当您使用严格等于运算符时不会发生这种情况===
:
11.9.6 The Strict Equality Comparison Algorithm
The comparison x === y, where x and y are values, produces true or false. Such a comparison is performed as follows:
- If Type(x) is different from Type(y), return false.
比较 x === y,其中 x 和 y 是值,产生真或假。这样的比较如下:
- 如果 Type(x) 与 Type(y) 不同,则返回false。
回答by erikkallen
Ah, the dreaded loose comparison operator strikes again. Never use it. Always use strict comparison, === or !== instead.
啊,可怕的松散比较运算符又来了。永远不要使用它。始终使用严格比较, === 或 !== 代替。
Bonus fact: 0 == ''
奖金事实: 0 == ''
回答by Stefan Nolde
Actually every object in javascript resolves to true if it has "a real value" as W3Cschools puts it. That means everything except ""
, NaN
, undefined
, null
or 0
.
实际上,如果 javascript 中的每个对象都像 W3Cschools 所说的那样具有“真正的价值”,那么它就会解析为 true。这意味着除了""
, NaN
, undefined
,null
或之外的所有内容0
。
Testing a number against a boolean with the ==
operator indeed is a tad weird, since the boolean gets converted into numerical 1 before comparing, which defies a little bit the logic behind the definition.
This gets even more confusing when you do something like this:
使用==
运算符针对布尔值测试数字确实有点奇怪,因为布尔值在比较之前被转换为数字 1,这有点违背定义背后的逻辑。当你做这样的事情时,这会变得更加混乱:
var fred = !!3; // will set fred to true
var joe = !!0; // will set joe to false
alert("fred = "+ fred + ", joe = "+ joe);
not everything in javascript makes a lot of sense ;)
并非 javascript 中的所有内容都有意义;)
回答by webHasan
When compare something with Boolean it works like following
当将某些东西与布尔值进行比较时,它的工作原理如下
Step 1: Convert boolean
to Number
Number(true) // 1
and Number(false) // 0
第 1 步:转换boolean
为Number
Number(true) // 1
和Number(false) // 0
Step 2: Compare both sides
第二步:比较双方
boolean == someting
-> Number(boolean) === someting
If compare 1
and 2
with true
you will get the following results
如果比较1
和2
与true
你会得到以下结果
true == 1
-> Number(true) === 1
-> 1 === 1
-> true
And
和
true == 2
-> Number(true) === 1
-> 1 === 2
-> false
回答by afr0
Well == is a joke comparision operator and as a js developers we must not be using == for comparison. All of the == comparison questions invalid to start with.
== 是一个笑话比较运算符,作为 js 开发人员,我们不能使用 == 进行比较。所有 == 比较问题一开始都是无效的。
回答by Hymantheripper
Use ===
to equate the variables instead of ==
.
用于===
使变量相等而不是==
。
==
checks if the value of the variables is similar
==
检查变量的值是否相似
===
checks if the value of the variables and the type of the variables are similar
===
检查变量的值和变量的类型是否相似
Notice how
注意如何
if(0===false) {
document.write("oh!!! that's true");
}?
and
和
if(0==false) {
document.write("oh!!! that's true");
}?
give different results
给出不同的结果
回答by Hyman
with == you are essentially comparing whether a variable is falsey when comparing to false or truthey when comparing to true. If you use ===, it will compare the exact value of the variables so true will not === 1
使用 == 您本质上是在比较变量与 false 或 true 比较时是否为 false 与 true 比较。如果您使用 ===,它将比较变量的确切值,因此 true 不会 === 1