判斷語句/分支語句??1.if else?和 if else if?else語句
??2.switch case語句
//if語句
/*
格式:if(條件){
語句;
}else if(條件){
語句;
}else{
語句;
}
*/
輸入年齡進(jìn)行范圍判斷
<body><input type="text" name="age"><input type="button" id="btn" value="提交"><script>
var btn = document.querySelector('#btn');
btn.onclick = function () {
let age = parseInt(document.querySelector('input[name="age"]').value);
if (age < 18) {
console.log("未成年");
} else if (age <= 35) {
console.log("中年");
} else {
console.log("老年人");
}
}
</script></body>
??判斷語句:switch case?
//switch語句
/*
意思:根據(jù)表達(dá)式的值匹配不同的case分支并執(zhí)行相應(yīng)的代碼
格式:switch(表達(dá)式的值){
case 比較的值:
執(zhí)行的語句;
break;
···
default:
執(zhí)行的語句;
}
*/
根據(jù)今天周幾進(jìn)行更換背景顏色
var date = new Date();var weekday = date.getDay(); switch (weekday) {
case 0:
console.log("7");
break;
case 1:
console.log("1");
break;
case 2:
console.log("2");
break;
case 3:
console.log("3");
break;
case 4:
console.log("4");
break;
case 5:
console.log("5");
break;
default:
console.log("6");
}
colorselect = ["red", "green", "yellow", "blue", "purple", "pink"];document.body.style.backgroundColor = colorselect[weekday];
循環(huán)語句/遍歷語句
??1.while循環(huán)
??2.for循環(huán)
??循環(huán)語句:while
//while語句
/*
格式:
while(條件){
語句;
}
*/
//示例一:
var liList = ["guohan","gh","gg","hh"];
var num = 0;
while (num<liList.length){
console.log(liList[num++]);
/*
console.log(liList[num]);
num++;
*/
}
//示例二:
var number = 1;
while(number<=10){
console.log(number++);
}
循環(huán)語句:for
//for循環(huán)
/*
//三種格式:
1.循環(huán)代替while:
for(變量初始化;判斷條件;步進(jìn)器){
語句;
}
2.遍歷數(shù)組成員的下標(biāo)或?qū)ο髮傩?/span>
for(變量(成員下標(biāo))in 數(shù)組){
語句;
}
3.遍歷數(shù)組成員的值或?qū)ο髮傩缘闹?/span>
for(變量(成員的值) of 數(shù)組){
語句;
}
*/
補(bǔ)充:forEach(數(shù)組的內(nèi)置方法): 遍歷數(shù)組的每個(gè)元素并對每個(gè)元素進(jìn)行一次指定的函數(shù)(回調(diào)函數(shù))
//數(shù)組.forEach((當(dāng)前元素,當(dāng)前下標(biāo),原數(shù)組)=>{函數(shù)代碼語句;});
var obj = ["guohan","gh","gg","hh"];
obj.forEach((item,key)=>{console.log(item)}) //數(shù)組.forEach((當(dāng)前元素,當(dāng)前下標(biāo),原數(shù)組)=>{函數(shù)代碼語句;});//里面是匿名函數(shù)新寫法
//obj.forEach(item=>{console.log(item)});
異常處理和主動拋出異常
//異常處理
1.拋出內(nèi)置異常
格式:
try{
代碼;
}catch(e){
代碼; //如:console.log(`異常類型:${e.name},異常原因:${e.message}`);
}
2.主動拋出自定義異常
//自定義異常用函數(shù)定義
function 自定義異常函數(shù)名(message){
this.name = "(自定義的錯(cuò)誤類型)";
this.message = message || ”默認(rèn)信息錯(cuò)誤"; //后面是防止 throw時(shí)忘記傳入錯(cuò)誤信息參數(shù)
}
try {
// 可能拋出異常的代碼(包含 throw)
if (條件不滿足) {
throw 自定義異常函數(shù)名(message); // 主動拋出異常
}
// 正常邏輯(如果沒拋異常,會執(zhí)行這里)
} catch (error) {
// 捕獲到異常后執(zhí)行的處理邏輯
console.error("捕獲到異常:", error.message);
} finally {
// 可選:無論是否拋出異常,都會執(zhí)行的代碼(如清理操作)
console.log("操作結(jié)束");
}
*/
拋出異常:
//拋出內(nèi)置異常
try{
console.log(num);
}catch(e){
console.log(e.name,e.message); //e.name:異常類型 e.message:異常原因
}finally{
console.log("操作完畢")
}
//主動拋出自定義異常 throw
try {
console.log(num);
} catch (e) {
console.log(`異常類型=${e.name},異常原因=${e.message}`); //異常類型=ReferenceError,異常原因=num is not defined
}
//主動拋出自定義異常 throw
function UserError(message) {
this.name = "userException";
this.message = message || "默認(rèn)錯(cuò)誤信息";
}
Person = {"name": "guohan", "age": 17};
try{
if (Person.age < 18){
throw new UserError("未成年禁止進(jìn)入");
}
console.log("可以進(jìn)入");
}catch(e){
console.log(e.name,e.message);
}finally{
console.log("操作完畢");
}


與python區(qū)別:

時(shí)間相關(guān):


該文章在 2025/11/1 9:27:18 編輯過