Reputation: 155
In JavaScript, if you have an if
… else if
… else
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
Reputation: 104
No it won’t check if the next condition is true, since you are using else if
.
Instead, if you use if
… if
… if
, then all if
statement will be executed.
Upvotes: 9
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