Conditional statement in js
Branch and loop statement
if statement
var age = prompt("请输入您的年龄:");
if( age<18 ){
alert("你还未成年");
}else if(age>100){
alert("bu");
}
else{
alert("你成年");
}
prompt() pops up the input box, click OK to return to the input content, click cancel to return to null
alert() pops up a warning dialog
string.length is
used to get the length of string, the return value is number
var password = prompt("请输入您的密码:");
if( password.length != 6 ){
alert("are you sure ,密码6位数字");
}else{
if( isNaN(password)==true){
alert("纯数字");
}else{
alert("输入成功");
}
}
A small chestnut
var str="abcl23";
var num parseInt(str) ;
if(num==NaN){
// NaN和任何内容都不相等,包括它本身
alert(NaN) ;
}else if(num==123){
alert( 123);
}else if(typeof num=="number" ){
alert( "num" ) ;
}else{
alert ( "str" );
// num为答案啊
document.write()
doucument.write("content");
output content to the browser
Switch is used to judge multiple conditions
switch(expression){
case value:statement
break;
case value:statement
break;
default:statement
}
Get the day of the week
new Date().getDay()
Get the day of the week
Return value: number(0~6)
var week = new Date().getDay();
var weekstr = "";
switch(week){
case 0:weekstr="日";
break;
case 1:weekstr="一";
break;
case 2:weekstr="二";
break;
case 3:weekstr="三";
break;
case 4:weekstr="四";
break;
case 5:weekstr="五";
break;
default:weekstr="六";
}
document.write("今天是星期"+weekstr);
The loop statement in js
for for-in while do...while
for(var i=1;i<=100;i++){
document.write(i+"<br />");
}
When loops and loops are nested, follow the following rules:
1. The inner layer is not executed when the outer layer is false;
2. The outer layer is executed first and then the inner layer is executed until the condition of the inner layer is false and then the outer layer is executed.
var i=1;
while(i<=100){
document.write(i+"<br />");
i+=2;
}
var i=1;
do{
document.write(i+"<br />");
i+=3;
}while(i<=100);
The difference between for and while loop
for loop is suitable
for knowing the number of loops while is suitable for knowing the condition, the loop of unknown number
break Immediately exit the loop
continue to end this loop and continue to start the next loop
NEXT:
JS function