JavaScript 中的“elseif”语法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4005614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:06:41  来源:igfitidea点击:

"elseif" syntax in JavaScript

javascriptif-statementsyntax

提问by Hari Gillala

Is this correct?

这样对吗?

if(condition)
{

} 
elseif(condition)
{

}
else
{

}

回答by Jeff

JavaScript's elseif is in the format "else if", e.g.:

JavaScript 的 elseif 格式为“else if”,例如:

if (condition) {

} else if (other_condition) {

} else {

}

回答by jMyles

Just add a space:

只需添加一个空格:

if (...) {

} else if (...) {

} else {

}

回答by Tamlyn

You could use this syntax which is functionally equivalent:

您可以使用此功能等效的语法:

switch (true) {
  case condition1:
     //e.g. if (condition1 === true)
     break;
  case condition2:
     //e.g. elseif (condition2 === true)
     break;
  default:
     //e.g. else
}

This works because each conditionis fully evaluated before comparison with the switchvalue, so the first one that evaluates to truewill match and its branch will execute. Subsequent branches will not execute, provided you remember to use break.

这是有效的,因为condition在与switch值比较之前每个都被完全评估,所以评估为的第一个true将匹配并且其分支将执行。后续分支将不会执行,前提是您记得使用break.

Note that strictcomparison is used, so a branch whose conditionis merely "truthy" will notbe executed. You can cast a truthy value to truewith double negation: !!condition.

请注意,使用了严格比较,因此不会执行condition仅“真实”的分支。您可以使用双重否定将真实值转换为:。true!!condition

回答by skube

Actually, technicallywhen indented properly, it would be:

实际上,从技术上讲,正确缩进时,它将是:

if (condition) {
    ...
} else {
    if (condition) {
        ...
    } else {
        ...
    }
}

There is no else if, strictly speaking.

else if严格来说,没有。

(Update: Of course, as pointed out, the above is notconsidered good style.)

(更新:当然,正如所指出的,以上被认为是好的风格。)

回答by IdemeNaHavaj

if ( 100 < 500 ) {
   //any action
}
else if ( 100 > 500 ){
   //any another action
}

Easy, use space

简单,使用空间

回答by A.A Noman

Conditional statements are used to perform different actions based on different conditions.

条件语句用于根据不同的条件执行不同的操作。

Use ifto specify a block of code to be executed, if a specified condition is true

使用if指定的代码块将被执行,如果一个指定的条件是真

Use elseto specify a block of code to be executed, if the same condition is false

使用else指定的代码块将被执行,如果相同的条件为假

Use else ifto specify a new condition to test, if the first condition is false

使用else if指定一个新的条件测试,如果第一个条件为假

回答by zloctb

x = 10;
if(x > 100 ) console.log('over 100')
else if (x > 90 ) console.log('over 90')
else if (x > 50 ) console.log('over 50')
else if (x > 9 ) console.log('over 9')
else console.log('lower 9') 

回答by codemirror

You are missing a space between elseand if

你在else和之间缺少一个空格if

It should be else ifinstead of elseif

它应该else if代替elseif

if(condition)
{

} 
else if(condition)
{

}
else
{

}