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

173
Visualizações
JS: proper way of using optional chaining?

I am just trying to understand at what level of an object to use optional chaining. If I have the given object

foo: {
    other: {
        name: "Bill"
    }
}

rather than do this:

if (foo && foo.other && foo.other.name) { ... }

I want to use optional chaining but I always finding myself just adding ?. at every level.

would the proper way be to add it to every level like this:

if (foo?.other?.name) {

}

or can you leave the last one out since that will result in false if it's not there:

if (foo?.other.name) {

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

0

You'll need to add the ? at every level where the preceding key can return a null value.

const ohNo = this?.data?.set?.can?.break?.at?.any?.level;

If you were to leave out any ? in there, you'd get null reference exceptions when the preceding depth level is 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