代码之家  ›  专栏  ›  技术社区  ›  Hari Gillala

JavaScript中的“elseif”语法

  •  255
  • Hari Gillala  · 技术社区  · 14 年前

    if(condition)
    {
    
    } 
    elseif(condition)
    {
    
    }
    else
    {
    
    }
    
    7 回复  |  直到 11 年前
        1
  •  495
  •   Jeff    14 年前

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

    if (condition) {
    
    } else if (other_condition) {
    
    } else {
    
    }
    
        2
  •  49
  •   Inanc Gumus    7 年前

    只需添加一个空格:

    if (...) {
    
    } else if (...) {
    
    } else {
    
    }
    
        3
  •  32
  •   Tamlyn    9 年前

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

    这样做是因为 condition switch 值,因此第一个计算为 true 将匹配并执行其分支。后续分支将不会执行,前提是您 remember to use break .

    请注意 严格的 条件 只是“真实的”意志 被处决。你可以把真实的价值观 真的 双重否定: !!condition .

        4
  •  12
  •   skube    6 年前

    技术上 如果缩进正确,它将是:

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

    没有 else if ,严格地说。

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

        5
  •  6
  •   IdemeNaHavaj    12 年前
    if ( 100 < 500 ) {
       //any action
    }
    else if ( 100 > 500 ){
       //any another action
    }
    

        6
  •  2
  •   A.A Noman    8 年前

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

    使用 if 如果指定的条件为true,则指定要执行的代码块

    else 如果相同条件为false,则指定要执行的代码块

    使用 else if

        7
  •  1
  •   zloctb    6 年前
    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') 
    
        8
  •  0
  •   codemirror    6 年前

    你在 else if

    应该是 else if 而不是 elseif

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