for-in
用于遍历key不是symbol类型的对象
for (const propName in window) {document.write(propName);}
for-of
用于iterable的对象
for (const el of [2,4,6,8]) {document.write(el);}
label, break, continue
break跳出当前循环,continue跳出本次循环继续下一次循环
let num = 0;outermost:for (let i = 0; i < 10; i++) {for (let j = 0; j < 10; j++) {if (i == 5 && j == 5) {break outermost; //用label指定要跳出的循环// continue outermost;}num++;}}console.log(num); // 95
with(不推荐)
// location是对象with(location) {let qs = search.substring(1);let hostName = hostname;let url = href;}// 等价于let qs = location.search.substring(1);let hostName = location.hostname;let url = location.href;
switch
switch用的是===,不会发生类型转换
switch (i) {case 25:/* falls through */ // 不加break则执行下一行case 35:console.log("25 or 35");break;case 45:console.log("45");break;default:console.log("Other");}
