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

240
Visualizações
how to return a value to variable if varible path returns undefined?

I have a variable and I want to return the value 0 if the original path returns as undefined.

I tried to do this with an IF statement inside the variable but this doesn't work.

Thank you for any help

const balance = wallet.balance if (balance === undefined || balance === null) {  
console.log("Balance is null or undefined"); 
balance = 0;
};

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

0

The easiest way would be using the nullish coalescing operator ??:

const balance = wallet.balance ?? 0

Something closer to what you already wrote would be using a ternary:

const balance = wallet.balance == null ? 0 : wallet.balance

(Note that wallet.balance == null with loose comparison is a shorter way to check for both null and undefined at once.)

Of course you could also write an if, but then it would have be at its own line - and the variable would have to be non-const so it can be modified:

let balance = wallet.balance
if (balance == null) {  
  console.log("Balance is null or undefined");
  balance = 0
}
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