[英]Is true == 1 and false == 0 in JavaScript?
I was reading a good book on JavaScript.我正在阅读一本关于 JavaScript 的好书。
It started with:它始于:
Boolean type take only two literal values: true and false.
布尔类型仅采用两个字面值:true 和 false。 These are distinct from numeric values, so true is not equal to 1, and false is not equal to 0.
它们与数值不同,因此 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 中的每种类型都有一个等价的布尔值。
But then, what's the truth?但是,真相是什么?
It's true that true
and false
don't represent any numerical values in Javascript. true
, false
不代表Javascript 中的任何数值。
In some languages (eg 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 (eg Pascal, C#), there is a distinct boolean type that is not numerical.在其他一些语言(例如 Pascal、C#)中,有一个不同的布尔类型不是数字。 It's possible to convert between boolean values and numerical values, but it doesn't happen automatically.
可以在布尔值和数值之间进行转换,但它不会自动发生。
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
. ==
相等运算符很高兴地在类型之间转换以找到匹配项,因此1 == true
评估为 true 因为true
转换为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
计算结果为 false,因为值的类型不同。
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.对于真正的相等,请使用 === 运算符。
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. ==
运算符执行从一种类型到另一种类型的转换,而===
运算符则不然。
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.
比较 x == y,其中 x 和 y 是值,产生真或假。 Such a comparison is performed as follows:
这样的比较如下:
- If Type(y) is Boolean, return the result of the comparison x == ToNumber(y) .
如果 Type(y) 是布尔值,则返回比较结果x == ToNumber(y) 。
Thus, in, if (1 == true)
, true
gets coerced to a Number
, ie 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
11.9.6 严格相等比较算法
The comparison x === y, where x and y are values, produces true or false.
比较 x === y,其中 x 和 y 是值,产生真或假。 Such a comparison is performed as follows:
这样的比较如下:
- If Type(x) is different from Type(y), return false .
如果 Type(x) 与 Type(y) 不同,则返回false 。
Ah, the dreaded loose comparison operator strikes again.啊,可怕的松散比较运算符又来了。 Never use it.
永远不要使用它。 Always use strict comparison, === or !== instead.
始终使用严格比较, === 或 !== 代替。
Bonus fact: 0 == ''
额外的事实:
0 == ''
Actually every object in javascript resolves to true if it has "a real value" as W3Cschools puts it.实际上,如果 javascript 中的每个对象都像 W3Cschools 所说的那样具有“真正的价值”,那么它就会解析为 true。 That means everything except
""
, NaN
, undefined
, null
or 0
.这意味着除
""
、 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.使用
==
运算符针对布尔值测试数字确实有点奇怪,因为布尔值在比较之前被转换为数字 1,这有点违背定义背后的逻辑。 This gets even more confusing when you do something like this:当你做这样的事情时,这会变得更加混乱:
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 中的所有内容都有意义;)
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
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给出不同的结果
Well == is a joke comparision operator and as a js developers we must not be using == for comparison. 那么==是一个笑话比较运算符,作为一个js开发人员,我们不能使用==进行比较。 All of the == comparison questions invalid to start with.
所有==比较问题都无效。
with == you are essentially comparing whether a variable is falsey when comparing to false or truthey when comparing to true.使用 == 您本质上是在比较变量与 false 或 true 比较时是否为 false 与 true 比较。 If you use ===, it will compare the exact value of the variables so true will not === 1
如果您使用 ===,它将比较变量的确切值,因此 true 不会 === 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.