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

224
Views
Use only the variable that caused an if statement to trigger, within the if statement

say you have an if statement:

try {
    if (!a || !b || !c || !d) {
      let nullVariable = ???;
      // use the variable that is null
      throw nullVariable
    }
} catch (ex) {
  log.debug(`${ex} is not defined`);
}

Is there a built in way to see which variable was set to null, without creating an if statement for each individual variable?

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

0

You can put the assignment inside the if condition expression.

let a, b, c, d;
a = 3;
b = "foo";
d = {x: 10};
let nullVariable = (!a && 'a') || (!b && 'b') || (!c && 'c') || (!d && 'd');
if (nullVariable) {
    console.log(`${nullVariable} is not defined`);
}

about 4 years ago · Juan Pablo Isaza Report

0

You can extract the variable name using object shorthand.

const fn = ({ a, b, c, d }) => {
  let nullVariable = Object.entries({ a, b, c, d }).find(([_, val]) => !val)?.[0];
  
  if(nullVariable)
    console.log(nullVariable + ' is falsy');
};

fn({ a: 1, c: 2, d: 3 });
fn({ a: 1, b: 2, c: 3 });
fn({ a: 1, b: 2, c: 3, d: 4 });

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!