null >= 0 == true?

null >= 0 == true?

Let's explore why null behaves differently with relational operators in Javascript.

First of all, before diving into complex relational expressions, it is necessary to understand what null actually is.

What is null?

null actually means no value. In javascript, typeof null is an object. So we can consider null as a special object which has no value and can be assigned to any variable. I personally consider null as a special value that can be anything but does not really point to some particular value or literal.

Now since we will be comparing and checking for equality, let's take a quick look at the == and === operators, and how they work in JS.

The == and === operators

Both == and === operators check for equality of two values, but the === the operator is strict for type checking as well unlike the == operator.

Another point that needs to be noted, is that the == operator does implicit type coercion.

Type Coercion in JS

Type coercion is the automatic or implicit conversion of values from one data type to another (such as strings to numbers). Type conversion is similar to type coercion because they both convert values from one data type to another with one key difference — type coercion is implicit whereas type conversion can be either implicit or explicit.

In Javascript, null is converted to undefined and undefined is converted to NaN, i.e., not-a-number which actually is of type number.

Implicit type coercion is only performed when both the values in comparison are not of the same type.

What is undefied?

Undefined is a type. A variable that has not been assigned any value is of type undefined.

Now let's see some examples

Let us start with the easiest one, and we will keep the above statement in mind while solving the questions.

  1. null == null

    If we just consider the above statement, then in that context we are just comparing the same object with itself. Hence, the expression is evaluated as true.

  2. null == 0

    Here, null is converted to undefined, which is then converted to NaN, which of course isn't 0. Hence, the expression is evaluated as false.

  3. null != 0

    This expression holds true, because of the same type of conversion to NaN which is not-equal-to 0.

    1. null > 0

      This particular expression is tricky and evaluated in a very different way from the above one. Under the hood, the comparison operators- > < >= <= return a boolean value, hence all of these do an implicit type conversion to make the two operands of the same type, hence it converts null to a number; i.e., Number(null) which returns 0.

      So the above expression can also be understood as 0 > 0, which of course is false.

      Now we know that null > 0 and null == 0 both evaluate to false. So the next one is going to be pretty easy to comprehend.

  4. null >= 0

    This is the most confusing of all, but not anymore. Let's see step by step.

    • First, >= converts null to 0.

    • So the expression becomes 0 >= 0.

    • The >= operator is the Greater-than-or-equal-to operator,i.e., the L.H.S can either be greater than or equal to the R.H.S of the expression. Which indeed is true for the expression 0 >= 0.

    • A similar viewpoint can be applied to the following expressions:

      null <= 0, and (null >= 0 && null <= 0) which all evaluate as true.

  5. null >= ""

This can be slightly tricky at first glance, but if thought well enough, we can clearly see that both null and "" will be converted to 0, hence true again.

Here is a link to a page that very highlights the comparisons and type coercions that happen in JS.

So how would null and undefined compare? Let's find out.

null == undefined and null === undefined

We already know how the == and === operators work, and we also know that null is implicitly converted to undefined.

So == since it does not check for type, will evaluate as true, as the expression would turn out to be, undefined == undefined.

Whereas === will strictly reject the expression as both null and undefined are of two different types.

Conclusion

  • Comparison operators return a boolean value.

  • When values of different types are compared, they get converted to numbers (with the exclusion of a strict equality check).

  • The values null and undefined equal == each other and do not equal any other value.

  • Be careful when using comparisons like > or < with variables that can occasionally be null/undefined. Checking for null/undefined separately is a good idea.