Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

480
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda