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

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

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