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

199
Visualizações
Is it necessary to use null two times at the end of the code?

I have one question. Help me, please.

I have code in my teaching proggram:

alert(user.address ? user.address.street ? user.address.street.name : null : null); 

But I can't understand, why he used "null" two times at the end of the code?

I understand that if user.adress - exist, then check whether user.address.street exist, if user.address.street - exist, then check whether user.address.street.name exist, if not alert - null.

But why did he write second null?

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

0

The ? operator is a shorthand for an if-else assignment.

alert(user.address ? user.address.street ? user.address.street.name : null : null);

Is the short form for:

let res;
if (user.address) {
    if (user.address.street) {
          res = user.address.street.name;
    } else {
          res = null;
    }
} else {
    res = null;
}
alert(res);

In javascript there is also the 'optional chaining operator' which is probably what you want:

alert(user?.address?.name);

Which only access the objects properties if they are not null, otherwise returns null.

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