Home > front end >  Why is test >= 0 true when test is null?
Why is test >= 0 true when test is null?

Time:02-10

Let's say I have the following javascript code:

const test = null

if ( test >= 0 ){
    console.log("Hello World")
}

This code will print Hello World... In my mind, it should work only if test === null. But all the times that I use the condition test >= 0 the code falls on it. Is there any reason why it happens? I see it as a problem in some situations... For example on the next code:

const test = null

if ( test >= 0 ){
    console.log("1")
} 
else if ( test === null ){
    console.log("Hello World")
}

In this case, the code is not doing what apparently it should be doing. The workaround to make it work would be having the habit of always putting the === null condition at first on if/else or switch/cases. Is that how I'm supposed to work with Javascript? Or is there a better way of doing it?

CodePudding user response:

Is there any reason why it happens?

Any value that is compared to a number will be coerced to a number1. Number(null) is 0, and 0 >= 0 is true. If you had used undefined on the other hand, you'd have gotten NaN >= 0 which is false.

1: In fact, the only non-numeric relational comparison is between two strings (or two objects that convert to strings).

The workaround to make it work would be having the habit of always putting the === null condition at first.

Yes, that's how to deal with variables that can have the value null.

Or is there a better way of doing it?

You might want to avoid having the value null at all in your variable. Depending on the application, whatever the value is representing, could also be expressed as 0 or -1 maybe? Those may not be much cleaner, but they would at least have predictable behaviour in >= 0.

CodePudding user response:

I thought it was a simple question but I was wrong. I went check ECMAScript Language Specification, which specified how "a>=b" works.

It turned out it is not doing "a>b || a==b", but instead, it is returning the oppsite answer of "a<b". I think that is why (null >= 0) is returning true, because (null < 0) is returning false.

ES3 spec

11.8.4 The Greater-than-or-equal Operator ( >= ) The production RelationalExpression : RelationalExpression >= ShiftExpression is evaluated as follows:

  1. Evaluate RelationalExpression.
  2. Call GetValue(Result(1)).
  3. Evaluate ShiftExpression.
  4. Call GetValue(Result(3)).
  5. Perform the comparison Result(2) < Result(4). (see 11.8.5).**
  6. If Result(5) is true or undefined, return false. Otherwise, return true.

And, I found another reference which plots the Relational and Equality Operators of some special cases.

Relational and Equality Operators

CodePudding user response:

if you cast null to bool, you are getting 0.

if ( test >= 0 ){

Here larger or equal operator using for comparison, so you are getting correct results.

For example

Boolean(null); // false

For more please check https://javascript.info/ifelse#boolean-conversion

  •  Tags:  
  • Related