跳轉到內容

JavaScript/保留字/case

來自華夏公益教科書,自由的教科書
上一頁: byte 保留字 下一頁: catch

thecase關鍵字

[編輯 | 編輯原始碼]

thecase關鍵字用於 switch 語句,它是複雜 if … else if … else 語句的替代方案。

程式碼
  var array = [2, 3, 5, 7, 10, 11];

  for (var i = 0; i < array.length; i++) {
    switch (array[i]) {
    case 0:
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
    case 7:
    case 8:
    case 9:
      alert(array[i] + " is a number with one digit.");
      break;
    default:
      alert(array[i] + " is a number with several digits.");
      break;
    }
  }
返回以下內容
2 is a number with one digit.
3 is a number with one digit.
5 is a number with one digit.
7 is a number with one digit.
10 is a number with several digit.
11 is a number with several digit.


另請參閱

[編輯 | 編輯原始碼]
上一頁: byte 保留字 下一頁: catch
華夏公益教科書