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

183
Views
Function calls with the arguments amount based on condition

I have a function called 'func':

const otherFunc = (arg1, arg2) => {...}
const func = (condition1, condition2) => {
 condition1 || condition2 ? otherFunc(value, true) : otherFunc(false)
}

The previous way works, but i'm wondering if is there is a way to avoid using two different calls to otherFunc. I tried this but is not correct syntax:

const func = (condition1, condition2) => {
 otherFunc((condition1 || condition2) && ...[value, true])
}

Edit: It is a simple function to chain if and elses:

function is(value, done) {
  return {
    else: (v) => is(done ? value : v, done),
    if: (condition) => (done || condition ? is(value, true) : is(null)),
    get: value,
  };
}
const n = 50000;
console.log(
  is('small')
    .if(n < 1000) 
    .else('big')
    .if(n > 10000)
    .else('medium-big')
    .if(n > 5000)
    .else('medium-small').get,
);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If you can put the arguments into an array, you can then alternate between the arrays to spread into the argument list.

otherFunc(
  ...((condition1 || condition2) ? [value, true] : [false])
);

That said, it isn't very readable. I'd really prefer

if (condition1 || condition2) {
  otherFunc(value, true);
} else {
  otherFunc(false);
}

Good maintainable code isn't a golfing competition - being as DRY as absolutely possible isn't always the best approach.

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!