Blogger Information
Blog 25
fans 1
comment 1
visits 17283
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
0224作业+JavaScript基础+10期线上班
江川林
Original
564 people have browsed it

JS是轻量级的脚本语言
-JS变量用var进行声明(6版本以下)
-JS变量名必须以字母,__(下划线),$开头,不能用数字开头
-JS脚本必须在<script></script>标签之间
-可以在<script>标签中用src属性加载JS文档

以下是变量声明与访问以及作用域的访问的代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>JavaScript</title>
  6. </head>
  7. <body>
  8. <script>
  9. // JS的变量声明与访问
  10. // 声明:var /let/const
  11. var age;
  12. // 变量赋值
  13. age = 20;
  14. console.log(age)
  15. // 声明加初始化
  16. var sex = '男';
  17. // 在浏览器的控制台显示
  18. console.log(sex);
  19. // 变量的连接用'+'表示
  20. var a = 'a' + 'b';
  21. console.log(a);
  22. console.log('变量是:' + a);
  23. // 只要变量不是在函数中生成的,均属于全局变量(属于windows对象下的属性)
  24. console.log(window.a);
  25. // JS变量的作业域与访问
  26. // JS和PHP一样支持函数作用域
  27. function f() {
  28. var b = 'haha';
  29. }
  30. // 在函数外部,不能访问函数内部的变量
  31. // console.log(b);
  32. </script>
  33. </body>
  34. </html>

以下是流程控制(分支与循环)的代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. <!--JS的分支和循环-->
  9. <script>
  10. // 单分支
  11. var a = 9;
  12. var b = '';
  13. if (a = 20) {
  14. b = '我有:' + a;
  15. }
  16. // console.log(b);
  17. // 双分支
  18. var a = 9;
  19. if (a > 10 && a < 30) {
  20. b = '单阶段';
  21. } else {
  22. b = '双阶段';
  23. }
  24. // console.log(b);
  25. // 多分支
  26. var a = 0;
  27. if (a > 0 && a < 10) {
  28. b = '一阶段';
  29. } else if (a >= 10 && a < 20) {
  30. b = '二阶段';
  31. } else if (a > 20 && a < 30) {
  32. b = '三阶段';
  33. } else {
  34. b = '没有此选项';
  35. }
  36. // console.log(b);
  37. // switch
  38. var a = 0;
  39. switch (true) {
  40. case a > 0 && a < 10:
  41. b = '一阶段';
  42. break;
  43. case a >= 10 && a < 20:
  44. b = '二阶段';
  45. break;
  46. case a > 20 && a < 30:
  47. break;
  48. default:
  49. b = '没有此选项';
  50. }
  51. // console.log(b);
  52. // 或者
  53. var a = 3;
  54. switch (a) {
  55. case 1:
  56. b = '哈哈';
  57. break
  58. case 2:
  59. b = '嘿嘿';
  60. break;
  61. default:
  62. b = '什么都没有';
  63. }
  64. // console.log(b);
  65. // for与while循环
  66. var a = 0;
  67. var b = 0;
  68. for (var i = 0; i < 5; i++){
  69. b += i;
  70. }
  71. // console.log(b);
  72. var i = 0;
  73. var b = 0;
  74. while (i<5){
  75. b += i;
  76. i++;
  77. }
  78. // console.log(b);
  79. // do-while
  80. var b = 0;
  81. var i = 0;
  82. do {
  83. b += i;
  84. i++;
  85. }while (i<5);
  86. // console.log(b);
  87. // break与continue
  88. var a = 0;
  89. var b = 0;
  90. for (var i = 0; i < 5; ++i){
  91. b += i;
  92. if (i === 2)break;
  93. }
  94. // console.log(b);
  95. var a = 0;
  96. var b = 0;
  97. for (var i = 0; i < 5; ++i){
  98. if (i === 2) continue;
  99. b += i;
  100. }
  101. console.log(b);
  102. </script>
  103. </body>
  104. </html>

简单来说,JS与PHP变量的区别就是,JS必须有声明语句,在作用域和流程控制方面是大同小异的

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!