js中switch語句的學習筆記
switch 語句用于基于不同條件執(zhí)行不同動作。
語法格式如下:
switch(表達式) {
case n:
代碼塊
break;
case n:
代碼塊
break;
default:
默認代碼塊
}
代碼解釋:
計算一次 switch 表達式;
把表達式的值與每個 case 的值進行對比;
如果存在匹配,則執(zhí)行關聯(lián)代碼。
如下:
switch (new Date().getDay()) {
case 0:
day = "星期天";
break;
case 1:
day = "星期一";
break;
case 2:
day = "星期二";
break;
case 3:
day = "星期三";
break;
case 4:
day = "星期四";
break;
case 5:
day = "星期五";
break;
case 6:
day = "星期六";
}
擴展內(nèi)容
JavaScript中switch語句的用法總結(jié)
JavaScript的switch...case語句,是在開發(fā)中經(jīng)常用到的,但是通常都是給定值,然后進入case分支的操作,今天來總結(jié)一些switch的其他操作。
var a = 100;
var b = NaN;
switch (true) {
case isNaN(a) || isNaN(b):
console.log('NaNNaN');
break;
case a === b:
console.log(0);
break;
case a < b:
console.log(-1);
break;
default:
console.log(1);
}
// NaNNaN
多case,單操作
var Animal = 'Giraffe';
switch (Animal) {
case 'Cow':
case 'Giraffe':
case 'Dog':
case 'Pig':
console.log('This animal will go on Noah\'s Ark.');
break;
case 'Dinosaur':
default:
console.log('This animal will not.');
}
// This animal will go on Noah's Ark.
到此這篇關于js中switch語句的學習筆記的文章就介紹到這了,更多相關js中switch語句的使用方法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
JavaScript實現(xiàn)繼承的6種常用方式總結(jié)
JavaScript想實現(xiàn)繼承的目的:重復利用另外一個對象的屬性和方法。本文為大家總結(jié)了JavaScript實現(xiàn)繼承的6種常用方式,需要的可以參考一下2022-07-07
JS中‘hello’與new String(‘hello’)引出的問題詳解
這篇文章主要給大家介紹了關于JS中'hello'與new String('hello')引出的問題的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面隨著小編來一起學習學習吧2018-08-08

