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

171
Visualizações
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 Respostas
Responde à pergunta

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 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