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

172
Vistas
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 Respuestas
Responde la pregunta

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