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

101
Views
Javascript switch case with multiple expression

I currently attempted to write a switch case taking in 3 variables

switch (user.nameInDB === userInputMobile,
        user.phoneInDB === userNumberInputMobile,
        user.emailInDB === userInputFrontend) {
   case ( true, _, _) :
      // Do something
   case ( _, true, _) :
      // Do something else
   case ( _, _, true) :
      // Do last thing
   default:
     break;
   }

I have ran into an error where the " _ " is not defined. Any advise on how to organise this?

enter image description here

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

0

If you're only checking for one variable at a time or maybe even more I recommend using If statments instead, It improves code readability and also has better performance than checking for multiple variables that aren't required to checked.

if (user.nameInDB === userInputMobile) {
   // do something
} 

However, If you wish to use switch statments you could just pass undefined, null or true/false depending on your use case.

about 4 years ago · Juan Pablo Isaza Report

0

As per the doc, switch statement evaluates an expression. So what you are trying to achieve

switch (expression1, expression2, expression3, ...)

Is the wrong way.

If you want to use switch for the same thing, you can use

const _ = false
const expression = [true, false, false].join()

switch(expression){
   case [true, _, _].join(): 
   // Do something
   case [_, true, _].join(): 
   // Do something
   case [_, _, true].join(): 
   // Do something
   default: 
   // Do something
}
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!