Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

477
Visualizações
Why is test >= 0 true when test is null?

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?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

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.

about 4 years ago · Juan Pablo Isaza Relatório

0

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

about 4 years ago · Juan Pablo Isaza Relatório

0

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

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda