JavaScript:`if (!x)` 和 `if (x == null)` 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5791158/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 18:52:08  来源:igfitidea点击:

JavaScript: What is the difference between `if (!x)` and `if (x == null)`?

javascript

提问by Xavier

What is the difference between if (!x)and if (x == null); that is, when can their results be different?

是有什么区别if (!x)if (x == null); 也就是说,他们的结果什么时候会有所不同?

回答by Felix Kling

!xwill return truefor every "falsy" value (empty string, 0, null, false, undefined, NaN) whereas x == nullwill only return trueif xisnull(edit:or apparently undefined(see below)).

!x将返回true每个“假”值(空字符串,0, null, false, undefined, NaN),而x == nulltruexnull编辑:或显然undefined(见下文))时返回。

Try with x = 0, there is a difference.

试试看x = 0,有区别。

You can say that the NOT operator !convertsa value into its opposite boolean equivalent. This is different than actually comparingtwo values.

您可以说 NOT 运算符!一个值转换为其相反的布尔值。这与实际比较两个值不同。

In addition, if you compare values with ==, JavaScript does type conversionwhich can lead to unexpected behavior (like undefined == null). It is better to always use strict comparison ===(value and type must be the same) and make use of type conversion only if you really know what you are doing.

此外,如果您将值与 进行比较==,JavaScript 会进行类型转换,这可能会导致意外行为(如undefined == null)。最好始终使用严格比较===(值和类型必须相同),并且只有在您真正知道自己在做什么时才使用类型转换。

Something to read:

一些阅读:



Update:

更新:

For more information about the non-strict comparison of nulland undefined(or the comparison in general), it is worth having a look at the specification. The comparison algorithm is defined there (the comparison is x == y):

有关nulland的非严格比较undefined(或一般比较)的更多信息,值得查看规范。比较算法在那里定义(比较是x == y):

  1. If Type(x) is the same as Type(y), then
    (...)
  2. If xis nulland yis undefined, return true.
  3. If xis undefinedand yis null, return true.
  4. (...)

(...)

  1. 如果 Type( x) 与 Type( y)相同,则
    (...)
  2. 如果Xÿ不确定的,返回
  3. 如果X未定义Ÿ,返回
  4. (……)

(……)

回答by Wesley Murch

The results can be different if x is false, NaN, '' (empty string), undefined (using the strict comparison operator ===), or 0 (zero).

如果 x 为假、NaN、''(空字符串)、未定义(使用严格比较运算符 ===)或 0(零),结果可能不同。

See Felix Kling's answerfor an excellent summary of type comparison.

有关类型比较的出色摘要,请参阅Felix Kling 的回答

回答by justkt

Say x is a string.

假设 x 是一个字符串。

x = undefined;
if(!x) {
   alert("X is not a truthy value");
}
if(x == null) {
   alert("X is null");
}

x = "";
if(!x) {
   alert("X is not a truthy value");
}
if(x == null) {
   alert("X is null");
}

x = null;
if(!x) {
   alert("X is not a truthy value");
}
if(x == null) {
   alert("X is null");
}

You'll notice that "X is not a truthy value" is shown in all three cases, but only in the case of X being undefined or null is "X is null" shown.

您会注意到在所有三种情况下都显示了“X 不是真值”,但只有在 X 未定义或为空的情况下才会显示“X 为空”。

When X is a boolean value, then (!x)will be true when X is false but (x == null)will not be. For numbers 0 and NaN are considered false values, so not X is truthy.

当 X 是布尔值时,则(!x)当 X 为假时为真,但(x == null)不会为真。对于数字 0 和 NaN 被认为是假值,所以不是 X 是真的。

See it in action, including the difference between ==(equality using type conversion) and ===(strict equality)

查看它的实际效果,包括==(使用类型转换的相等)和===(严格相等)之间的区别

回答by AngusC

if (!x) 

coerces x uses the internal ToBoolean function

强制 x 使用内部 ToBoolean 函数

if (x==null)

coerces both operands using the internal ToPrimitive function (which generally resolves each side to a number, occasionally a string, depending on the operands)

使用内部 ToPrimitive 函数强制两个操作数(通常将每一边解析为一个数字,有时是一个字符串,具体取决于操作数)

For full explanantion of ToBoolean vs ToPrimitive see http://javascriptweblog.wordpress.com/2011/02/07/truth-equality-and-javascript/

有关 ToBoolean 与 ToPrimitive 的完整说明,请参阅http://javascriptweblog.wordpress.com/2011/02/07/truth-equality-and-javascript/

回答by Thomas Clayson

!xtests for a false value. This will be true for any value that can propagate to false for whatever reason. This will be true for permutations of false, 0, etc etc.

!x测试错误值。对于因任何原因可以传播为 false 的任何值,这都是正确的。这将是真正的的排列false0等等等等。

x == nullis different because var x = 0will NOT be null... but WILL be false.

x == null是不同的,因为var x = 0不会为空......但会是假的。

回答by bob

This question/answer has some isTrue isFalse functions which may help folks:

这个问题/答案有一些 isTrue isFalse 函数,可以帮助人们:

forgiving isTrue isFalse

原谅 isTrue isFalse