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

155
Views
How to avoid a long variable exists check in Javascript without triggering the undefined error

I'm using an API which is cheap but badly designed in so that in order to know whether I'm getting any results at all, I have to do the following check:

const resultsExist = relatedKeywords.tasks[0].result[0].items ? true : false

However, there may be a case where even the .task part of relatedKeywords doesn't exist either.

So my question is — is there an elegant way to check the existence of the parameter items above without triggering the undefined error in Javascript?

So far, I'm doing it this way below, but I find it too verbose. Maybe there's a better way?

const resultsExist =
        relatedKeywords &&
        relatedKeywords.tasks &&
        relatedKeywords.tasks[0] &&
        relatedKeywords.tasks[0].result &&
        relatedKeywords.tasks[0].result[0] &&
        relatedKeywords.tasks[0].result[0].items
            ? true
            : false
about 4 years ago · Juan Pablo Isaza
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!