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

110
Vistas
Safe way to check the deep of object's property?
const object = {
    level1: {
      level2: {
        level3: 'foo'
      }
    }
  }

Let's say I want to get level3's value

console.log(object.level1.level2.level3);

That's so risky considering level1 and level2 could be null or undefined. Is there any simple way to get level3 value safely? Beside check the object properties one by one like this

if (object.level1) {
    if (object.level2) {
      if (object.level3) {
        // do stuff
      }
    }
  }
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Try This, "?."

const object = {
    level1: {
      level2: {
        level3: 'foo'
      }
    }
}

console.log('object: ',object?.level1?.level2?.level3)
about 4 years ago · Juan Pablo Isaza Denunciar

0

That is exactly why you have Optional chaining (introduced in ES2020) in javascript.

It's an operator (?.) which enables you to read the value of a property located deep within a chain of connected objects without having to check that each reference in the chain is null/undefined so it actually protects you from getting a null reference error.

console.log(object.level1?.level2?.level3);
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