Null >= 0?

1 the introduction

What do you think of null >= 0 as true? You can either settle for it, or follow me and find out.

2 Content Summary

Is greater than the judgment

When using javascript to determine a > b, remember the following 21 steps:

  1. Call b’s ToPrimitive(hit Number) method.
  2. Call the ToPrimitive(hit Number) method of A.
  3. If Result(1) and Result(2) are both strings, skip to Step 16.
  4. Call ToNumber (Result (1)).
  5. Call ToNumber (Result (2)).
  6. If Result(4) is NaN, return undefined.
  7. If Result(5) is NaN, return undefined.
  8. If Result(4) and Result(5) are the same number, return false.
  9. If Result(4) is +0 and Result(5) is -0, return false.
  10. If Result(4) is -0 and Result(5) is +0, return false.
  11. If Result(4) is +∞, return false.
  12. If Result(5) is +∞, return true.
  13. If Result(5) is -∞, return false.
  14. If Result(4) is -∞, return true.
  15. Return true if Result(4) is smaller than Result(5), otherwise return false.
  16. If Result(2) is a prefix of Result(1) return false.
  17. Return true if Result(1) is a prefix of Result(2).
  18. Find a position k such that a[k] is not equal to b[k].
  19. Take m as the value of the a[k] character.
  20. Take n as the value of the b[k] character.
  21. If m < n, return true, otherwise return false.

ToPrimitive takes precedence over existing values: valueOf(), toString(), and throws an exception if none exists. ToPrimitive(hit Number) Indicates the implicit value type

So null > 0 results in false.

Is equal to the judge

Now let’s look at what happens when a == b (the triple equals sign is the strictest type; the double equals sign is the most complicated case).

  1. If a is of the same type as B, then:
  • Return true if Type(b) is undefined.
  • Return true if Type(b) is null.
  • If Type(b) is number,
    • If b is NaN, return false.
    • If a is NaN, return false.
    • Return true if a and B have the same value.
    • Return true if a is +0 and b is -0.
    • Return true if a is -0 and b is +0.
    • Otherwise, return false.
  • Return true if Type(b) is string and a and b are identical strings, otherwise return false.
  • If Type(b) is Boolean, return true if both are true or false, otherwise return false.
  • Return true if a and B are the same object reference, otherwise return false.
  1. If a is null and b is undefined, return true.
  2. If a is undefined and B is null, return true.
  3. If Type(a) is number and Type(b) is string, return a == ToNumber(b).
  4. If Type(a) is string and Type(b) is number, ToNumber(a) == b is returned.
  5. If Type(a) is Boolean, return ToNumber(a) == b.
  6. If Type(b) is Boolean, return a == ToNumber(b).
  7. If Type(a) is string or number, and Type(b) is the object Type, return a == ToPrimitive(b).
  8. ToPrimitive(a) == b if Type(a) is the object Type and Type(b) is string or number.
  9. Otherwise, return false.

So null == 0 goes to step 10 and returns the default false.

Greater than or equal judgment

Javascript defines a greater than or equal judgment like this:

If a < b is false, then a >= b is true

So null >= 0 is true because null < 0 is false.

3 intensive reading

About toPrimitive

To expand, we can define the toPrimitive behavior of a class using Symbol. ToPrimitive, for example:

class AnswerToLifeAndUniverseAndEverything { [Symbol.toPrimitive](hint) { if (hint === 'string') { return 'Like, 42, man'; } else if (hint === 'number') { return 42; } else { // when pushed, most classes (except Date) // default to returning a number primitive return 42; }}}Copy the code

And not playing by the rules?

According to the above principle, we can draw inferences from one example:

{} >= {} // true
Copy the code

But why?

null >= {} // false
Copy the code

If ToPrimitive(hit Number) contains a NaN, it will return undefined and print false, while {} will return NaN and therefore false.

4 summarizes

NaN is a special thing in javascript, and only isNaN can tell exactly what NaN is, and when it is used for comparison, it simply returns false.

Javascript implicit conversions have a set of precedence rules, and implicit conversions of different values require table memory. There are also three tables, ToPrimitive(Hint Number)ToPrimitive(Hint String) ToPrimitive(Hint Boolean), which are a bit complicated to remember.

Therefore, it is recommended to use === when comparing variables. Strongly typed languages such as Typescript Flow constrain variable types and do not compare variables of different types.

The discussion address is:
Null >= 0? · Issue #36 · DT-fe /weekly


If you’d like to participate in the discussion, please
Click here to, with a new theme every week, released every Friday.