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

170
Vistas
ES12: Nullish coalescing operators & objects

For example I have an object like this:

const a = {
 b: "value"
 // and also what about: c: '', or c: 0, c: false ?
};

And I'd like to assign a 'c', key to my object, but only if it's not assigned before.

Usually we do something like this:

if (!a.c) {
  a.c = 1; // or without { } for a bit shorty way.
}

But ES12 standards introduce a bit more new Nullish coalescing and logical operators, so can someone explain to me how does it help me with replacing the example above and what about null and 0 as a number behavior (empty string and false behavior is also a plus)?

The real question behind it, is about: can use of this new feature really could cover all the cases and replace the example above in real-production projects, or is it still better to stay in a more traditional way. (Of, course it's all about syntax sugar staff, but I'd like to know more about coverage)

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

0

The logical nullish assignment ??= assigns the value only if the value of the left hand side is either undefined or null.

a.c ??= 1;

If you like to replace any falsy value, like '', 0, false, null, undefined, you could take the logical OR assignment ||=.

a.c ||= 1;
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