Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

javascript

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 
like image 429
Totty.js Avatar asked Nov 15 '12 22:11

Totty.js


People also ask

Is null equal to 0 in JavaScript?

Comparisons convert null to a number, treating it as 0 . That's why (3) null >= 0 is true and (1) null > 0 is false. On the other hand, the equality check == for undefined and null is defined such that, without any conversions, they equal each other and don't equal anything else. That's why (2) null == 0 is false.

IS null less than 0 JavaScript?

Example. In the following example, null is not greater than 0 and not equal to 0, but greater than or equal to 0. It looks very odd to hear. Because in mathematics if we have two numbers i.e a, b and if a is not less than the b then the possible scenarios are either a is greater than b or a is equal to b.

What is null equal to in JavaScript?

The value null represents the intentional absence of any object value. It is one of JavaScript's primitive values and is treated as falsy for boolean operations.

IS null == in JavaScript?

In JavaScript, == compares values by performing type conversion. Both null and undefined return false. Hence, null and undefined are considered equal.


2 Answers

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.

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 

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

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

§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. Call ToPrimitive(x, hint Number).

  2. Call ToPrimitive(y, hint Number).

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

  4. Call ToNumber(Result(1)).

  5. Call ToNumber(Result(2)).

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

§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. If Type(x) is different from Type(y), go to step 14.

...

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

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

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

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

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

19. If Type(y) is Boolean, return the result of the comparison 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).

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

22. Return false.

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

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

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

like image 185
John Kugelman Avatar answered Oct 14 '22 08:10

John Kugelman


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. == doesn't cast null to a number, so null == 0 is false.

like image 22
tmcw Avatar answered Oct 14 '22 10:10

tmcw