Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

109
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda