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
Javascript stops when first if condition fails, need to move it to second condition

I have the following code in which I ask it to check if either one of 2 dataLayer values is "!= false" then run the rest of the code.

However, code breaks as soon as the first condition fails and does not move to check the second condition. So, how can I let both conditions be checked as one of them will always be available in dataLayer?

function init() {
    if (dataLayer.condition[1] != false || dataLayer.condition[2] != false) {
        Do something
    }
    
}

Below is the screenshot of the error I get when the first condition values are missing on the page.

enter image description here

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use optional chaining (?.) for this, if your execution context is expected to support it:

if (dataLayer?.condition[1] != false || dataLayer?.condition[2] != false) {
    // Do something
}
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!