Javascript 为什么 (null == false) 和 (null == true) 都返回 false?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27632391/
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 (null == false) and (null == true) both return false?
提问by iatboy
I know that nullis an object with no attributes or functions.
我知道这null是一个没有属性或功能的对象。
However, I am confused that why console.log(null == false);and console.log(null == true);both return false.
但是,我对为什么console.log(null == false);和console.log(null == true);两者都返回 false感到困惑。
What are the conversion rules between nulland boolean?
null和之间的转换规则是boolean什么?
回答by Sean Vieira
This is because the Abstract Equality Comparison Algorithmrequires that if Type(x)or Type(y)is a Boolean in the expression x == ythen the Boolean value should be coerced to a number via ToNumber, which converts trueto 1 and falseto +0.
这是因为抽象相等比较算法要求如果表达式中的Type(x)或Type(y)是布尔值,x == y那么布尔值应该被强制ToNumber转换true为一个数字 via ,该数字转换为 1 和falseto +0。
This means that any comparison of true == somethingor something == trueresults in 1 == somethingor something == 1(replacing trueand 1with falseand +0for false).
这意味着,任何比较true == something或something == true导致1 == something或something == 1(取代true和1与false和+0为false)。
The Null typedoes not compare as equal to either 1 or +0 (in fact, null is only comparable to undefinedin the AECA).
Null 类型不等于 1 或 +0(实际上,null 仅可undefined与 AECA 中的值进行比较)。
There is a detailed discussion of all of the different kinds of equality in JavaScript on MDNthat is well worth looking at if you want to know more.
MDN上对 JavaScript 中所有不同类型的相等性进行了详细讨论,如果您想了解更多信息,非常值得一看。
However, if you coerce nullto a number it is coerced to +0so +null == falseactually returns true.
不过,如果你强迫null了一些它裹挟+0所以+null == false实际的回报true。
回答by Gayan C Liyanage
Answer : There no relative aspect between null and boolean.
答案:空值和布尔值之间没有相关的方面。
MDN Source:-
MDN 来源:-
The value null is a literal (not a property of the global object like undefined can be). In APIs, null is often retrieved in place where an object can be expected but no object is relevant. When checking for null or undefined beware of the differences between equality (==) and identity (===) operators (type-conversion is performed with the former).
// foo does not exist, it is not defined and has never been initialized: > foo "ReferenceError: foo is not defined" // foo is known to exist now but it has no type or value: > var foo = null; foo "null"Difference between null and undefined
typeof null // object (bug in ECMAScript, should be null) typeof undefined // undefined null === undefined // false null == undefined // true
值 null 是一个文字(不是像 undefined 这样的全局对象的属性)。在 API 中,通常在可以预期对象但没有相关对象的地方检索 null。检查 null 或 undefined 时,请注意相等 (==) 和身份 (===) 运算符之间的差异(类型转换是用前者执行的)。
// foo does not exist, it is not defined and has never been initialized: > foo "ReferenceError: foo is not defined" // foo is known to exist now but it has no type or value: > var foo = null; foo "null"null 和 undefined 的区别
typeof null // object (bug in ECMAScript, should be null) typeof undefined // undefined null === undefined // false null == undefined // true
回答by Diptendu
The value nullis a JavaScript literal represents an "empty" value or "undefined". nullis one of JavaScript's primitive values. It is neither equal to boolean truenor equal to boolean falsebecause it's value is undefined. The value of nullis more inclined towards false even though it is not false. That's why it's called "falsey" operators and an if (var) { }block does not get executed when varis null.
该值null是一个 JavaScript 文字,表示“空”值或“未定义”。null是 JavaScript 的原始值之一。它既不等于 booleantrue也不等于 booleanfalse因为它的值是未定义的。null即使不是 ,的值也更倾向于 false false。这就是为什么它被称为“falsey”运算符并且if (var) { }块在varis时不会被执行null。
回答by Victor Hernandez de Crespo
Adding to the current discussion. null >= falsereturns true.
添加到当前的讨论中。null >= false返回true。
I believe that it is because this is interpreted as !(null < false)
我相信这是因为这被解释为 !(null < false)

