Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

255
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda