1、if-else

  1. if (time<10)
  2. {
  3. document.write("<b>早上好</b>");
  4. }
  5. else if (time>=10 && time<20)
  6. {
  7. document.write("<b>今天好</b>");
  8. }
  9. else
  10. {
  11. document.write("<b>晚上好!</b>");
  12. }

2、switch

  1. var d=new Date().getDay();
  2. switch (d)
  3. {
  4. case 6:
  5. x="今天是星期六";
  6. break;
  7. case 0:
  8. x="今天是星期日";
  9. break;
  10. default:
  11. x="期待周末";
  12. }
  13. document.getElementById("demo").innerHTML=x;

3、for

  1. <script>
  2. let str = 'abcdefg';
  3. let arrays = [1, 2, "abc"];
  4. //for
  5. for (let i = 0; i < str.length; i++) {
  6. console.log('for:'+str[i]);
  7. }
  8. //for of
  9. for (let s of str) {
  10. console.log('for of:'+s);
  11. }
  12. //for in
  13. for (let key in arrays) {
  14. console.log('for in:'+arrays[key]);
  15. }
  16. //forEach
  17. arrays.forEach(element => {
  18. console.log('forEach:'+element);
  19. });
  20. </script>

5、while

while

  1. var i = 0;
  2. while (i < names.length) {
  3. console.log(names[i]);
  4. i++;
  5. }

do/while 循环

  1. var i = 0;
  2. do
  3. {
  4. x=x + "The number is " + i + "<br>";
  5. i++;
  6. }
  7. while (i<5);

6、break

中止for

  1. for (i=0;i<10;i++)
  2. {
  3. if (i==3) break;
  4. x=x + "The number is " + i + "<br>";
  5. }

7、continue

跳过这次for,继续下一个

  1. for (i=0;i<=10;i++)
  2. {
  3. if (i==3) continue;
  4. x=x + "The number is " + i + "<br>";
  5. }

8、return

直接返回