VinLucero
VinLucero

Reputation: 155

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?

Upvotes: 2

Views: 3937

Answers (2)

manoj
manoj

Reputation: 104

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.

Upvotes: 9

Azad Salam
Azad Salam

Reputation: 146

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.

Upvotes: 8

Related Questions