for-in

用于遍历key不是symbol类型的对象

  1. for (const propName in window) {
  2. document.write(propName);
  3. }

for-of

用于iterable的对象

  1. for (const el of [2,4,6,8]) {
  2. document.write(el);
  3. }

label, break, continue

break跳出当前循环,continue跳出本次循环继续下一次循环

  1. let num = 0;
  2. outermost:
  3. for (let i = 0; i < 10; i++) {
  4. for (let j = 0; j < 10; j++) {
  5. if (i == 5 && j == 5) {
  6. break outermost; //用label指定要跳出的循环
  7. // continue outermost;
  8. }
  9. num++;
  10. }
  11. }
  12. console.log(num); // 95

with(不推荐)

  1. // location是对象
  2. with(location) {
  3. let qs = search.substring(1);
  4. let hostName = hostname;
  5. let url = href;
  6. }
  7. // 等价于
  8. let qs = location.search.substring(1);
  9. let hostName = location.hostname;
  10. let url = location.href;

switch

switch用的是===,不会发生类型转换

  1. switch (i) {
  2. case 25:
  3. /* falls through */ // 不加break则执行下一行
  4. case 35:
  5. console.log("25 or 35");
  6. break;
  7. case 45:
  8. console.log("45");
  9. break;
  10. default:
  11. console.log("Other");
  12. }