JavaScript的分支語句
JavaScript條件語句
學(xué)習(xí)目標(biāo)
- 掌握條件語句 if
- 掌握prompt()的應(yīng)用
- 掌握alert()的應(yīng)用
if 語句
語法一:
if(condition){
statement1;
}
alert()
語法:alert()
功能:彈出警號對話框
語法二:
if(condition){
statement1;
}else{
statement2;
}
prompt()
語法:prompt()
功能:彈出輸入框
返回值:(1)點擊確定,返回輸入內(nèi)容
(2)點擊取消,返回null
例如:
var age=prompt("請輸入您的年齡");
if(age<18){
alert("您還沒有成年");
}else{
alert("您已成年,請?zhí)峁┠纳矸葑C號");
}
語法三:
if(condition){
statement1;
}else if(condiction){
statement2;
}...else{
statement3;
例如:
var age=prompt("請輸入您的年齡");
if(age<18){
alert("您還沒有成年");
}else if(age>=18 && age<=59){
alert("您可以進入");
}else{
alert("您已超出年齡限制");
}
if 語句的嵌套
length
語法:string.length
功能:獲取string字符串的長度
返回值:number
例如:
var password=prompt("請設(shè)置您的密碼");
// 判斷密碼的長度,如果不是6位,則
if(password.length!=6){
alert("請輸入6位數(shù)的密碼");
}else{
// 如果密碼是非數(shù)字,否則是數(shù)字
if(isNaN(password)==true){
alert("密碼必須是數(shù)字");
}else{
alert("密碼設(shè)置正確");
}
}