简体   繁体   English

为什么JavaScript中的null大于-1,小于1,但不等于(==)0? 那究竟是什么?

[英]Why is null in JavaScript bigger than -1, less than 1, but not equal (==) to 0? What is it exactly then?

From the Google Chrome console:从谷歌浏览器控制台:

var x = null;
undefined
x > 0
false
x < 0
false
x > -1
true
x < 1
true
x == 1
false
x === 1
false

When you compare null for equality to 0, the result is false. 将null的相等性与0进行比较时,结果为false。 If you force null to be interpreted in a numeric context then it is treated like 0 and the result becomes true. 如果强制在数字上下文中解释null ,则将其视为0,结果为true。

You can force it to be numeric by putting + in front, or by using numeric operators like < , <= , > , and >= . 您可以通过将+放在前面或使用数字运算符(如<<=>>=强制它为数字。 Notice how null >= 0 and null <= 0 are both true. 注意null >= 0null <= 0都是如此。

> null == 0
false
> +null == 0
true
> null >= 0
true
> null <= 0
true

The ECMAScript Language Specification defines when a so-called "ToNumber" conversion is performed. ECMAScript语言规范定义何时执行所谓的“ToNumber”转换。 When it is, null and false are both converted to 0. 如果是,则null和false都转换为0。

§9.1 Type Conversion and Testing : §9.1类型转换和测试

Table 14 — To Number Conversions 表14 - 至数字转换

\nArgument Type Result 参数类型结果\n------------- ------ ------------- ------\nUndefined Return NaN 未定义的返回NaN\nNull Return +0  回报+0\nBoolean Return 1 if argument is true. Boolean如果argument为true,则返回1。 Return +0 if argument is false. 如果参数为假,则返回+0。\nNumber Return argument (no conversion). Number返回参数(无转换)。\nString See grammar and note below. 字符串请参阅下面的语法和注释。\n

Knowing when the ToNumber conversion is applied depends on the operator in question. 知道何时应用ToNumber转换取决于所涉及的操作员。 For the relational operators < , <= , > , and >= see: 对于关系运算符<<=>>=请参阅:

§11.8.5 The Abstract Relational Comparison Algorithm : §11.8.5抽象关系比较算法

The comparison x < y , where x and y are values, produces true , false , or undefined (which indicates that at least one operand is NaN ). 比较x < y ,其中x和y是值,产生truefalseundefined (表示至少一个操作数是NaN )。 Such a comparison is performed as follows: 这样的比较如下进行:

  1. Call ToPrimitive(x, hint Number). 调用ToPrimitive(x,提示编号)。

  2. Call ToPrimitive(y, hint Number). 调用ToPrimitive(y,提示编号)。

  3. If Type(Result(1)) is String and Type(Result(2)) is String, go to step 16. (Note that this step differs from step 7 in the algorithm for the addition operator + in using and instead of or.) 如果Type(Result(1))是String并且Type(Result(2))是String,请转到步骤16.(请注意,此步骤与添加运算符+的算法中的步骤7的不同之处在于使用而不是或。 )

  4. Call ToNumber(Result(1)). 调用ToNumber(结果(1))。

  5. Call ToNumber(Result(2)). 调用ToNumber(结果(2))。

The == operator is different. ==运算符是不同的。 Its type conversions are described below. 其类型转换如下所述。 Notice how null and false follow different rules. 注意null和false如何遵循不同的规则。

§11.9.3 The Abstract Equality Comparison Algorithm §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: 这样的比较如下进行:

1. If Type(x) is different from Type(y), go to step 14. 1.如果类型(x)与类型(y)不同,请转到步骤14。

... ...

14. If x is null and y is undefined , return true . 14.如果x为null且y 未定义 ,则返回true

15. If x is undefined and y is null , return true . 15.如果x 未定义且y为null ,则返回true

16. If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y). 16.如果Type(x)为Number且Type(y)为String,则返回比较结果x == ToNumber(y)。

17. If Type(x) is String and Type(y) is Number, return the result of the comparison ToNumber(x) == y. 17.如果Type(x)是String而Type(y)是Number,则返回比较结果ToNumber(x)== y。

18. If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y. 18.如果Type(x)是布尔值,则返回比较结果ToNumber(x)== y。

19. If Type(y) is Boolean, return the result of the comparison x == ToNumber(y). 19.如果Type(y)是布尔值,则返回比较结果x == ToNumber(y)。

20. If Type(x) is either String or Number and Type(y) is Object, return the result of the comparison x == ToPrimitive(y). 20.如果Type(x)是String或Number而Type(y)是Object,则返回比较结果x == ToPrimitive(y)。

21. If Type(x) is Object and Type(y) is either String or Number, return the result of the comparison ToPrimitive(x) == y. 21.如果Type(x)是Object而Type(y)是String或Number,则返回比较结果ToPrimitive(x)== y。

22. Return false . 22.返回

If you read carefully you can see why false == 0 is true but null == 0 is false. 如果仔细阅读,可以看到为什么false == 0为true但null == 0为false。

  • For false == 0 , Type(x) is Boolean. 对于false == 0 ,Type(x)是布尔值。 This means Step 18's type conversion is applied, and false is converted to a number. 这意味着应用了步骤18的类型转换,并将false转换为数字。 ToNumber(false) is 0, and 0 == 0 is true, so the comparison succeeds. ToNumber(false)为0,0 0 == 0为真,因此比较成功。

  • For null == 0 , Type(x) is Null. 对于null == 0 ,Type(x)为Null。 None of the type checks match so the comparison falls through to Step 22, which returns false. 没有类型检查匹配,因此比较将进入步骤22,返回false。 The comparison fails. 比较失败。

null casts to 0 as a number: (+null) is 0. > and < cast null to this value, so when compared to numbers it acts as zero. null将数字转换为0作为数字: (+null)为0.>并且<cast null为此值,因此与数字进行比较时,它将作为零。 == doesn't cast null to a number, so null == 0 is false. ==不会将null转换为数字,因此null == 0为false。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM