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

197
Views
JS Convert function to one line without if/else

How to convert this function without using if/else?

function Menu() {
if (!currentUser || currentUser.getValue == 7)  {
    return false
} else {
    return true
}

}

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

0

Maybe just do ?

function validateMenu() {
  return p.currentUser && p.currentUser.getValue('id') !== 7
}

I really don't understand why would you want to use a callback or a Promise for this case. There is nothing asynchronous involved, only two simple conditions being checked.

You might want to read a bit more about callbacks and Promises.

about 4 years ago · Juan Pablo Isaza Report

0

It's

let showMenu = (!p.currentUser || p.currentUser.getValue('id') === 7) ? false : true;

If you want to use promises you can

function validateMenu() {
    return new Promises((resolve, reject) => {

        if (!p.currentUser || p.currentUser.getValue('id') === 7) {
            resolve(false)
        } else {
            resolve(true)
        }
    });
}
about 4 years ago · Juan Pablo Isaza Report

0

You don't need if...else statement or conditional (ternary) operator, you could just use arrow function expression and logical operators:

const Menu = () => !!currentUser && currentUser.getValue('id') !== 7;
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!