跳转到内容

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
华夏公益教科书