Blogger Information
Blog 30
fans 0
comment 1
visits 21952
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
0224 JS变量声明与访问及作用域流程控制
Admin
Original
565 people have browsed it

JS变量声明与访问及作用域流程控制

JS变量声明与访问 && JS变量作用域

JS的变量声明,var + 变量名;

  1. //变量的声明与访问
  2. var a;//变量的声明
  3. a = 1;//变量的赋值。
  4. var b = 10;//变量的声明与初始化
  5. var c = 10+2;
  6. console.log(c);
  7. console.log(a+' '+b);
  8. function x(){
  9. var test = '测试';
  10. }
  11. //JS中有函数作用域与全局作用域,你可以理解为function里面声明变量在全局则没有!
  12. console.log(test);


JS的作用域全局变量与函数作用域

JS流程控制

  1. <script>
  2. //JS流程控制
  3. //if
  4. if(true){
  5. console.log('我输出了');
  6. }
  7. if(false){
  8. console.log('true看到我');
  9. }else{
  10. console.log('false看到我');
  11. }
  12. var a = 1;
  13. if(false){
  14. console.log('true看到我');
  15. }else if(a === 1){
  16. console.log('如果a===1看到我');
  17. }
  18. var data = new Date();
  19. var week = data.getDay();
  20. var day = null;
  21. switch(week){
  22. case 0:
  23. day = '今天是周日';
  24. break;
  25. case 1:
  26. day = '今天是周一';
  27. break;
  28. case 2:
  29. day = '今天是周二';
  30. break;
  31. case 3:
  32. day = '今天是周三';
  33. break;
  34. case 4:
  35. day = '今天是周四';
  36. break;
  37. case 5:
  38. day = '今天是周五';
  39. break;
  40. case 6:
  41. day = '今天是周六';
  42. break;
  43. default:
  44. day = '世界线发生了变动';
  45. break;
  46. }
  47. console.log(day);
  48. var sumi = 0;
  49. for(var i=0;i<10;i++){
  50. console.log(sumi+=i);
  51. }
  52. var i=0;
  53. var sumi = 0;
  54. while(i<10){
  55. console.log(sumi+=i);
  56. i++;
  57. }
  58. var j = 0;
  59. do{
  60. console.log('我会输出一次!');
  61. j++;
  62. }while(j === 0);
  63. for(var i=0;i<5;i++){
  64. if(i === 1) break;//break是终止循环
  65. console.log(i);
  66. }
  67. for(var i=0;i<5;i++){
  68. if(i === 2) continue;//跳过本次循环
  69. console.log(i);
  70. }
  71. </script>

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:继续努力
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post