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

254
Views
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 answers
Answer question

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 Report

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 Report

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 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!