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

199
Views
Is there way to use the variable that tested true in an if statement

I can't explain it much in the title but I'm now stuck with having to test 2 variables for the same value and was wondering is there a way to do something like this. var a = 'abc';
var b = 'def'; if (a=='abc' || b=='abc'){ c = 'z'; }

Now c is the variable that was tested true, so by assigning a value to c it would assign the value to the variable that tested true in the if statement. a and b can have different values but both can have the same value aswell.

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

0

No, the expression just yields a result value, with no information stored about which subexpression was truthy. (And separately, JavaScript doesn't have references to variables [at least, not that you can use in your code].) You'll have to break it out into two ifs:

if (a === "abc") {
    a = "z";
} else if (b === "abc") {
    b = "z";
}

It would be possible to do something like this if the variables were accessor properties (or other function calls) with side effects (since the right-hand operand of the || won't be evaluated at all if the left-hand operand evaluates to a truthy value). Or if they were properties in an object and you tested an array of property names, etc. But it would be unnecessarily complicated. :-)

about 4 years ago · Juan Pablo Isaza Report

0

You could take a switch statement, here you can check various variables agains a certain value.

switch ('abc') {
    case a:
        a = 'z';
        break;
    case b:
        b = 'z';
        break;
}
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!