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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With