Blogger Information
Blog 37
fans 0
comment 0
visits 14208
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
javascript数据类型和分支循环
秋闲独醉
Original
232 people have browsed it
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>javascript数据类型和分支循环</title>
  8. </head>
  9. <body>
  10. <script>
  11. // javascript数据类型分为
  12. // 1、原始类型: Number String Boolean undefined null
  13. // 2、引用类型:array object function
  14. // number
  15. console.log("这是number:" + 123);
  16. //string
  17. console.log("这是string:" + "123");
  18. //boolean
  19. console.log("这是boolean:" + false);
  20. //undefined
  21. let a;
  22. console.log("这是undefined:" + a);
  23. //null
  24. let b = null;
  25. console.log("这是null:" + b);
  26. //array
  27. let arr = ["小明", "男", "16", "16cm"];
  28. console.log("这是array:" + arr);
  29. let obj1 = [
  30. { name: "手机", num: 2, price: 5000 },
  31. { name: "电脑", num: 5, price: 6000 },
  32. { name: "相机", num: 4, price: 2000 },
  33. ];
  34. console.log("这是array:" + obj1[0].name);
  35. //object
  36. let obj = {
  37. name: "Tom",
  38. age: 18,
  39. height: "180cm",
  40. fun: function () {
  41. console.log("这是一个function.");
  42. },
  43. };
  44. console.log("这是一个object:" + obj["age"]);
  45. console.log("这是一个object:" + obj.age);
  46. obj.fun();
  47. //查看数据类型用typeof
  48. console.log(typeof function () {});
  49. console.log(arr instanceof Object);
  50. //回调函数
  51. function f1() {}
  52. function f2(f1) {
  53. console.log("这是回调函数" + typeof f1);
  54. }
  55. f2(f1);
  56. //这是闭包函数
  57. function f3() {
  58. return function () {
  59. return "hello world";
  60. };
  61. }
  62. console.log("这是闭包函数" + f3()());
  63. </script>
  64. <script>
  65. //分支和循环
  66. // 1、单分支
  67. if (true) {
  68. console.log("这是一个单分支");
  69. }
  70. //2、双分支
  71. if (false) {
  72. console.log("这个是双分支,真的");
  73. } else {
  74. console.log("这个是双分支,假的");
  75. }
  76. //3、三元运算
  77. let res;
  78. isNaN(123) ? (res = "是") : (res = "不是");
  79. console.log("这" + res + "三元运算");
  80. //多分支
  81. let age = 20;
  82. if (age < 18) {
  83. console.log("年龄小于18岁");
  84. } else if (age >= 18 && age < 25) {
  85. console.log("正是青春年华");
  86. } else if (age >= 25) {
  87. console.log("差不多了");
  88. }
  89. //switch判断分支
  90. //必须要用true做为switch入口判断条件
  91. switch (true) {
  92. case age < 18:
  93. console.log("年龄小于18岁");
  94. breack;
  95. case age >= 18 && age < 25: {
  96. console.log("正是青春年华");
  97. break;
  98. }
  99. default: {
  100. console.log("差不多了");
  101. }
  102. }
  103. //for循环,for of 循环 ,for in 循环
  104. arr = [9, 4, 2, 7, 4];
  105. obj = {
  106. product: "电视",
  107. num: 3,
  108. price: 300,
  109. fun: function () {},
  110. };
  111. //for循环
  112. for (let i = 0; i < arr.length; i++) {
  113. console.log("for循环:" + arr[i]);
  114. }
  115. //for of 循环
  116. for (let item of arr) {
  117. console.log("for of循环:" + item);
  118. }
  119. for (let item of arr.values()) {
  120. console.log("for of循环值:" + item);
  121. }
  122. for (let item of arr.keys()) {
  123. console.log("for of循环键:" + item);
  124. }
  125. //for in 循环
  126. for (let key in obj) {
  127. console.log("for in循环键:" + key);
  128. }
  129. //这里只能发obj[key]访问,不能用obj.key .obj[key],key不能有引号
  130. for (let key in obj) {
  131. console.log("for in循环值:" + obj[key]);
  132. }
  133. </script>
  134. </body>
  135. </html>
Correcting teacher:PHPzPHPz

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