三元运算符多重判断

#三元运算符的多重判断

// 判断是否属于光的三原色(红,蓝,绿)
  var color = "black"; //red,blue,green
  // if...else if...else
  if (color == "red") {
    console.log("红色-if")
  } else if (color == "blue") {
    console.log("蓝色-if")
  } else if (color == "green") {
    console.log("绿色-if")
  } else {
    console.log("不属于光的三原色-if")
  };
  //  switch,
  // 使用的是严格相等的运算符(===)不会转换类型
  // 在比较number与string,number与boolean时需要注意
  switch (color) {
    case "red":
      console.log("红色-switch")
      break;
    case "blue":
      console.log("蓝色-switch")
      break;
    case "green":
      console.log("绿色-switch")
      break;
    default:
    console.log("不属于光的三原色-switch")
  }
  // 三元(此处进行了换行)
  color=="red"?console.log("红色-三元")
  :color=="blue"?console.log("蓝色-三元")
  :color=="green"?console.log("绿色-三元")
  :console.log("不属于光的三原色-三元")

#使用black来试验是否进入了判断
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/sixsixsix_6000/article/details/107360802
今日推荐