标签:类型转换 mes 布尔值 含义 val expec div expected 数据类型
它的好处是可以取代出现各种if else if的判断语句
1 var num = 8; 2 switch (num){ 3 case 8: 4 alert(8); 5 num=7; 6 console.log(num); 7 break; 8 case 1:alert(1); 9 break; 10 case 2:alert(2); 11 break; 12 case 3: 13 //合并num等于3或4的情况 14 case 4:alert(3,4); 15 break; 16 case 5:alert(5); 17 break; 18 default:alert("不匹配"); 19 break; 20 }
1 switch ("hello world") { 2 case "hello" + " world": 3 alert("Greeting was found."); 4 break; 5 case "goodbye": 6 alert("Closing was found."); 7 break; 8 default: 9 alert("Unexpected message was found."); 10 }
case判断布尔值:
1 var num = 25; 2 switch (true) { 3 case num < 0: 4 alert("Less than 0."); 5 break; 6 case num >= 0 && num <= 10: 7 alert("Between 0 and 10"); 8 break; 9 case num > 10 && num <= 20: 10 alert("Between 10 and 20"); 11 break; 12 default: 13 alert("More than 20"); 14 }
每一份case返回一个布尔值,故switch传达的表达式是true;
switch 语句在比较值时使用的是全等操作符,因此不会发生类型转换(例如,
字符串 "10" 不等于数值 10)
标签:类型转换 mes 布尔值 含义 val expec div expected 数据类型
原文地址:https://www.cnblogs.com/LeeeOooonHuang/p/11493034.html