为什么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?

查看:122
本文介绍了为什么JavaScript中的null大于-1,小于1,但不等于(==)等于0?那到底是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Google Chrome控制台中:

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

推荐答案

当将等于的null与0进行比较时,结果为false.如果强制null在数字上下文中进行解释,则将其视为0,结果变为true.

When you compare null for equality to 0, the result is false. If you force null to be interpreted in a numeric context then it is treated like 0 and the result becomes true.

您可以通过将+放在前面或使用数字运算符(例如<<=>>=)来将其强制为数字.请注意null >= 0null <= 0都是正确的.

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 == 0
false
> +null == 0
true
> null >= 0
true
> null <= 0
true


ECMAScript语言规范定义了何时执行所谓的"ToNumber"转换.如果为null,则将null和false都转换为0.


The ECMAScript Language Specification defines when a so-called "ToNumber" conversion is performed. When it is, null and false are both converted to 0.

§ 9.1类型转换和测试:

表14 —到数字的转换

§9.1 Type Conversion and Testing:

Table 14 — To Number Conversions


Argument Type     Result
-------------     ------
Undefined         Return NaN
Null              Return +0
Boolean           Return 1 if argument is true. Return +0 if argument is false.
Number            Return argument (no conversion).
String            See grammar and note below.

知道何时应用ToNumber转换取决于所讨论的运算符.有关关系运算符<<=>>=,请参见:

Knowing when the ToNumber conversion is applied depends on the operator in question. For the relational operators <, <=, >, and >= see:

§ 11.8.5抽象关系比较算法 :

比较x < y(其中x和y是值)产生 true false , 或未定义(表示至少一个操作数为 NaN ).这样的 比较如下:

§11.8.5 The Abstract Relational Comparison Algorithm:

The comparison x < y, where x and y are values, produces true, false, or undefined (which indicates that at least one operand is NaN). Such a comparison is performed as follows:

  1. 调用ToPrimitive(x,提示编号).

  1. Call ToPrimitive(x, hint Number).

调用ToPrimitive(y,提示编号).

Call ToPrimitive(y, hint Number).

如果Type(Result(1))为String且Type(Result(2))为String,请转到步骤16. 使用and代替or的加法运算符+.)

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.)

呼叫ToNumber(Result(1)).

Call ToNumber(Result(1)).

呼叫ToNumber(Result(2)).

Call ToNumber(Result(2)).

==运算符是不同的.其类型转换如下所述.请注意null和false如何遵循不同的规则.

The == operator is different. Its type conversions are described below. Notice how null and false follow different rules.

§ 11.9.3抽象平等比较算法

比较x == y(其中x和y是值)产生 true .这样的比较如下:

§11.9.3 The Abstract Equality Comparison Algorithm

The comparison x == y, where x and y are values, produces true or false. Such a comparison is performed as follows:

1.如果Type(x)与Type(y)不同,请转到步骤14.

1. If Type(x) is different from Type(y), go to step 14.

...

14.如果x为 null ,而y为未定义,则返回 true .

14. If x is null and y is undefined, return true.

15.如果x为未定义,而y为,则返回 true .

15. If x is undefined and y is null, return true.

16.如果Type(x)为Number,Type(y)为String,则返回比较结果x == ToNumber(y).

16. If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).

17.如果Type(x)为String,Type(y)为Number,则返回比较结果ToNumber(x)== y.

17. If Type(x) is String and Type(y) is Number, return the result of the comparison ToNumber(x) == y.

18.如果Type(x)为布尔型,则返回比较结果ToNumber(x)== y.

18. If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.

19.如果Type(y)为布尔型,则返回比较结果x == ToNumber(y).

19. If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).

20.如果Type(x)是String或Number,而Type(y)是Object,则返回比较结果x == ToPrimitive(y).

20. If Type(x) is either String or Number and Type(y) is Object, return the result of the comparison x == ToPrimitive(y).

21.如果Type(x)是Object并且Type(y)是String或Number,则返回比较结果ToPrimitive(x)== y.

21. If Type(x) is Object and Type(y) is either String or Number, return the result of the comparison ToPrimitive(x) == y.

22.返回 false .

如果仔细阅读,您会发现为什么false == 0为true但null == 0为false.

If you read carefully you can see why false == 0 is true but null == 0 is false.

  • 对于false == 0,Type(x)是布尔值.这意味着将应用步骤18的类型转换,并且将false转换为数字. ToNumber(false)为0,并且0 == 0为true,因此比较成功.

  • For false == 0, Type(x) is Boolean. This means Step 18's type conversion is applied, and false is converted to a number. ToNumber(false) is 0, and 0 == 0 is true, so the comparison succeeds.

对于null == 0,类型(x)为空.没有类型检查匹配,因此比较进入步骤22,该步骤返回false.比较失败.

For null == 0, Type(x) is Null. None of the type checks match so the comparison falls through to Step 22, which returns false. The comparison fails.

这篇关于为什么JavaScript中的null大于-1,小于1,但不等于(==)等于0?那到底是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆