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

94
Views
Nested if statements with or condition

I have some js code that currently looks something like this:

var test
//do some stuff with the var
if (test === "A" || test === "B" || test === "C") {
    if (test === "A" || test === "B") {
        //do some small stuff
    } 
    //do some more complex stuff
}

Is there a nicer way to write this to avoid the duplicated condition? It seems basic, I know, but I can't quite put my finger on it.

about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

you could assign a variable to avoid having to write the complex condition multiple times:

let a_or_b = test === 'A' || test === 'B';
if (a_or_b || test === 'C') {
    if (a_or_b) {
        // do some small stuff
    }
    // do more complex stuff
}

or

let a_or_b = false;
if (test === 'A' || test === 'B') {
    // do some small stuff
    a_or_b = true;
}
if (a_or_b || test === 'C') {
    // do more complex stuff
}
about 4 years ago · Santiago Gelvez Report

0

My suggestion, just set the duplicated condition into as a variable, hope it help you

var test;
var isAOrB = (test === "A" || test === "B");
if (isAOrB || test === "C") {
if (isAOrB) {
    //do some small stuff
} 
//do some more complex stuff

}

about 4 years ago · Santiago Gelvez 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!