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

259
Visualizações
How to exit or break out from an else if

I've searched for a couple stackoverflow questions although couldn't find my answer.

I'm trying to break from an else if statement and was wondering if there was a more efficient way.

Heres a snippet:

var argument = "something";

if(argument == 'not_this'){
  // this doesn't trigger
} else if(argument){
  // this triggers although if the functions in here doesn't match what I want, 
  // how do I make It skip to the else statement without adding another else
  // if statement? 
} else {
  // do something if both statements above fail
}

Is there something that I can do which exits from the else if(argument)... without adding another else statement? I've tried using switch and case although those don't seem to help.

Thanks.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You could set a flag that is by default true, and whenever the argument is valid you set it to false.

Then, to know when to execute the 'else' block, you can just check whether the flag is true or not:

var argument = "something";
let invalid = true

if (argument == 'not_this') {
  // this doesn't trigger
  invalid = false
} else if (argument) {
  if (typeof argument != 'string') {
    //valid
    invalid = false
  }
}

if (invalid) {
  console.log('invalid')
}

about 4 years ago · Juan Pablo Isaza Relatório

0

Restructure the code to avoid confusing states, move out the if 'has value' check.

if (argument) {
  if (argument === 'not_this') {
    // this doesn't trigger
  }
} else {
  // do something if both statements above fail
}

For equality, it is safer to use strict equal === instead of loose equal ==

about 4 years ago · Juan Pablo Isaza Relatório

0

You could try using return; to break out of the statement, as that would stop all other code from being read.

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