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

232
Vistas
Any difference between x || [] and x ?? [] in javascript?

I've see both x || [] and x ?? [] used for providing a fallback value if x is nullish. Are there any cases where these two give different results?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

If x was a non-nullish falsey value, it would be different.

x = 0;
x = x ?? []
console.log(x);



y = null;
y = y ?? []
console.log(y);

about 4 years ago · Juan Pablo Isaza Denunciar

0

These expressions x || [] and x ?? [] are logical assignments in Javascript.

x ?? [] is used to represent null or undefined cases while x || [] represent true if either a or b is true.

  • x ?? [] works by evaluating if the left side of the expression is null or undefined.

  • x || [] works by evaluating if a or b is true. If a is true continue in the if statement. If b is true, continue in the if statement.

about 4 years ago · Juan Pablo Isaza Denunciar

0

They do different things.

?? is called Nullish coalescing operator https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing_operator

And works checking if the left side of the evaluation is null or undefined and if it's not the case will assign the right side of the evaluation.

The previous is really useful when you are checking a variable with the value zero which will be false if you are checking for it.

|| is the Logical Operator https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_OR

The logical operator works as any other logical operator. Will evaluate the left side and then the right side

null || 1 // output 1
undefined || 1 // output 1
0 || 1 // 1 <-- there are cases where you want to treat zero as a truthy value and **that's where the nullish operator is handy**
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