Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

108
Views
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 answers
Answer question

0

Try This, "?."

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

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

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!