简体   繁体   English

在第一个条件为真之后,JavaScript是否会检查“ if”-“ else if”-“ else”中的其余条件?

[英]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? 在JavaScript中,如果遇到ifelse ifelse情况,如果计算机先前触发了另一个条件,计算机是否继续检查下一个条件? Eg if the first if condition is true and the code executes, does it still attempt to check the conditions for else if or else ? 例如,如果第一if条件为真,代码执行,它仍试图检查的条件else if还是else

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. 如果a为true,则将调用func1()并在返回之后,控件将转到下a语句。

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. 因此,如果b为true,则将首先func2() ,然后在返回后,控件将转到下一条语句。

func4() will be called if a , b , c all of them are false. 如果abc均为假,则会调用func4() After func4() returns control goes to next statement. func4()返回后,控制权转到下func4()语句。

No it won't check if the next condition is true, since you are using else if . 不,它不会检查下一个条件是否成立,因为您正在使用else if

Instead, if you use ififif , then all if statement will be executed. 相反,如果使用ififif ,则将执行所有if语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM