Blogger Information
Blog 17
fans 1
comment 0
visits 8759
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
JS数据类型和遍历循环
P粉933302309
Original
345 people have browsed it

1.数据类型

数据类型 1.原始类型:number(数值),string(字符串),Boolean; 2.引用类型:array,object,function;
演示如下

  1. // 数值
  2. console.log(1111);
  3. // 字符串
  4. console.log("文字");
  5. // boolean
  6. console.log(typeof true, typeof false);
  7. // array
  8. let a;
  9. console.log(a);
  10. // object
  11. console.log(typeof null);
  12. // undefined
  13. console.log(typeof undefined);
  14. let c = 4564646;
  15. console.log(typeof c);
  16. // 修改数据类型
  17. c = "字符串";
  18. console.log(typeof c);
  19. // 2.引用类型
  20. const arr = ["文字", 2222, 2000];
  21. console.table(arr);
  22. console.log(arr[0]);
  23. console.log(arr[1]);
  24. // 对象
  25. const obj = {
  26. name: "老王",
  27. id: 1111,
  28. price: 20000,
  29. total: function () {
  30. return this.name + "价格:" + this.id * this.price + "元";
  31. },
  32. };
  33. console.log(obj.total());

2.分支和循环

  1. // 分支与循环
  2. console.log("---------------------------------------------");
  3. // 单分支
  4. y = 20;
  5. if (y > 11) {
  6. console.log("大了大了");
  7. } else {
  8. console.log("小了小了");
  9. }
  10. // 三元操作
  11. let y2 = y > 11 ? true : false;
  12. console.log(y2);
  13. console.log("---------------------------------------------");
  14. // 多分支
  15. y = 10;
  16. if (y > 10) {
  17. console.log("大了大了");
  18. } else if (y < 200 && y > 30) {
  19. console.log("大于10");
  20. } else if ((y = 10)) {
  21. console.log("刚刚好");
  22. } else {
  23. console.log("查询失败!");
  24. }
  25. y = 30;
  26. switch (true) {
  27. case (y = 10):
  28. console.log("等于10");
  29. break;
  30. case y < 10:
  31. console.log("小于10");
  32. break;
  33. case y < 100 || y > 20:
  34. console.log("大于20小于100");
  35. break;
  36. default:
  37. console.log("未知");
  38. }
  39. console.log("-------------------------------------------------");
  40. // 循环
  41. const age = [111, 222, 333];
  42. // 索引初始化
  43. let i = 0;
  44. // 循环条件
  45. let length = age.length;
  46. if (i < length) {
  47. console.log(age[i]);
  48. // 更新循环条件
  49. i = i + 1;
  50. }
  51. if (i < length) {
  52. console.log(age[i]);
  53. i = i + 1;
  54. }
  55. if (i < length) {
  56. console.log(age[i]);
  57. i = i + 1;
  58. } else {
  59. console.log("结束");
  60. }
  61. console.log("--------------------------");
  62. // while
  63. i = 0;
  64. // 入口判断
  65. while (i < length) {
  66. console.log(age[i]);
  67. i++;
  68. }
  69. i = 0;
  70. // 出口判断
  71. do {
  72. console.log(age[i]);
  73. i++;
  74. } while (i < length);
  75. // for循环
  76. for (i = 0; i < length; i++) {
  77. console.log(age[i]);
  78. }
  79. for (const item of age) {
  80. console.log(item);
  81. }
  82. // 对象
  83. const ddd = {
  84. a: 1,
  85. b: 2,
  86. c: function () {},
  87. };
  88. for (let key in ddd) {
  89. console.log(ddd[key]);
  90. }
  91. </script>
  92. </body>
  93. </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