Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does JavaScript check remaining conditions in “if”–“else if”–“else” after the first condition is true?

In JavaScript, if you have an ifelse ifelse situation, does the computer keep checking the next condition if it previously triggered another condition? E.g. if the first if condition is true and the code executes, does it still attempt to check the conditions for else if or else?

like image 552
VinLucero Avatar asked Dec 01 '25 05:12

VinLucero


2 Answers

No it won’t check if the next condition is true, since you are using else if.

Instead, if you use ififif, then all if statement will be executed.

like image 181
manoj Avatar answered Dec 03 '25 18:12

manoj


Say you have a code segment

if(a)
  func1()

else if(b)
  func2()

else if(c)
 func3()

else
 func4();

//next statement

If a is true func1() will be called and, after it returns, the control will go to next statement.

Accordingly if b is true then the at first func2() will be called and then, after it returns, the control will go to next statement.

func4() will be called if a, b, c all of them are false. After func4() returns control goes to next statement.

like image 37
Azad Salam Avatar answered Dec 03 '25 20:12

Azad Salam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!