if语句
if(true){
console.log('yes');
}else{
console.log('no');
}
while语句
let i = 0
while(i < 10){
i += 2;
}
console.log(i)
do-while语句
let i = 0;
do{
i += 2;
}while(i < 10)
console.log(i)
for语句
for (let i = 0; i < 5; i++) {
console.log(i);
}
for-in语句
for(const propName in window){
document.write(propName);
}
for-of语句
for (const el of [2,4,6,8]) {
document.write(el);
}
break和continue语句
let num = 0;
for (let i = 0; i < 5; i++) {
if(i == 2) break;
num++;
}
console.log(num);
let num = 0;
for (let i = 0; i < 5; i++) {
if(i == 2) continue;
num++;
}
console.log(num);
with语句(不使用!!!)
let qs = location.search.substring(1);
let url = location.href();
等价于
with(location){
let qs = search.substring(1);
let url = href;
}
with语句影响性能难于调试,不推荐使用!!
swith语句
let i = 66;
switch(i){
case 11:
console.log('11');
case 66:
console.log('66');
case 77:
console.log('77');
}
let i = 66;
switch(i){
case 11:
console.log('11');
break;
case 66:
console.log('66');
break;
case 77:
console.log('77');
break;
}
为了不必要的判断,最好在每个条件后面都加上break语句。
|