Blogger Information
Blog 11
fans 0
comment 0
visits 6568
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
javascript 中的数据类型及语句的运用
Feel Lonely
Original
350 people have browsed it

一、实例演示 javascript 中的各种数据类型

  1. 基础类型(值类型):字符串(String)、数字(Number)、布尔(Boolean)、空(Null)、未定义(Undefined);

  2. 对象类型(引用类型):对象(Object)、数组(Array)、函数(Function)。

代码部分
  1. <script>
  2. //我是字符串
  3. let a = 'abc';
  4. console.log(a);
  5. console.log(typeof a + ',变量a' + ':是字符串');
  6. console.log('________________________');
  7. // 我是数值
  8. let b = 10;
  9. console.log(b);
  10. console.log(typeof b + ',变量b' + ':是数值');
  11. console.log('________________________');
  12. // 我是布尔值
  13. let c = true;
  14. let d = false;
  15. console.log(c + ' 和 ' + d);
  16. console.log(typeof c + '、' + typeof d + ',变量c和变量d' + ':是布尔值');
  17. console.log('________________________');
  18. // 我是未定义的
  19. let e;
  20. console.log(e);
  21. console.log(typeof e + ',变量e' + ':是未定义默认值');
  22. console.log('________________________');
  23. // 我是空值
  24. let f = null;
  25. console.log(f);
  26. console.log(typeof f + ',变量f' + ':是空值');
  27. console.log('________________________');
  28. // 我是数组
  29. let g = ['苹果', '香蕉']
  30. console.log(g);
  31. console.log(g[0], g[1]);
  32. console.log(typeof g + ',变量g' + ':是数组');
  33. console.log('________________________');
  34. // 我是对象
  35. let h = {
  36. 姓名: "张三",
  37. 国籍: "中国",
  38. 年龄: 38,
  39. }
  40. console.log(h);
  41. console.log('姓名:' + h.姓名 + '、' + '国籍:' + h.国籍 + '、' + '年龄:' + h.年龄 + '岁');
  42. console.log(typeof h + ',变量h' + ':是对象');
  43. console.log('________________________');
  44. // 我是函数
  45. function i(x, y) {
  46. z = x + y;
  47. return z;
  48. }
  49. i('PHP中文网,', '加油!')
  50. console.log(z);
  51. console.log(typeof i + ',变量i' + ':是函数');
  52. </script>
浏览器效果

二、实例演示 javascript 中的常用语句

2.1 if语句
  1. <script>
  2. let i = 120;
  3. if (i >= 100) {
  4. console.log('大爷,你都100了,赶紧回家谢这区吧!');
  5. }
  6. </script>

2.2 if…else语句
  1. <script>
  2. let i = 45;
  3. if (i >= 60) {
  4. console.log('还不错及格了!');
  5. } else {
  6. console.log('考的啥玩意,及格线都没过!');
  7. }
  8. </script>

2.2 do…while语句
  1. <script>
  2. // 小明搬砖头
  3. // 一天要搬2000块结束,用变量“s”表示
  4. // 一趟搬10块 用常量“l”表示
  5. // 问搬多少趟才能搬完?
  6. // 趟数用变量“i”表示
  7. let i = 0 ;
  8. const l = 10;
  9. do {
  10. i ++;
  11. s = i * l;
  12. } while (s < 2000);
  13. console.log(i);
  14. </script>

2.3 while语句
  1. <script>
  2. let s = 0;
  3. let i = 0;
  4. const l = 10;
  5. while (s < 2000) {
  6. i++
  7. s = l * i;
  8. }
  9. console.log(i);
  10. </script>

2.4 for语句
  1. <script>
  2. const l = 10;
  3. let s = 0;
  4. for (i = 0; s < 2000; i++) {
  5. console.log(i);
  6. s = l * i;
  7. }
  8. </script>

2.5 for…of语句

可以通过:values(值)、keys(键)、entries(键和值)获取键名和键值。

  1. <script>
  2. let car = ['奥迪','奔驰','宝马']
  3. for (let item of car) {
  4. console.log(item);
  5. }
  6. </script>

2.6 for…in语句
  1. <script>
  2. // 我是数组
  3. let car = ['奥迪', '奔驰', '宝马']
  4. // 我是对象
  5. let obj = {
  6. 苹果:8000,
  7. 华为:5000,
  8. 小米:3000,
  9. }
  10. for (let key in car) {
  11. console.log(car[key]);
  12. }
  13. for (let key in obj) {
  14. console.log(obj[key]);
  15. }
  16. </script>

2.7 for…in语句
  1. <script>
  2. let a = 90;
  3. switch (true) {
  4. case a < 60:
  5. console.log("你好垃圾");
  6. break;
  7. case a >= 60 && a < 80:
  8. console.log("成绩还行");
  9. break;
  10. case a >= 80 && a < 90:
  11. console.log("成绩优秀");
  12. break;
  13. case a >= 90 && a < 100:
  14. console.log("你是别人家的孩子");
  15. break;
  16. default:
  17. console.log("你牛逼上天了");
  18. }
  19. </script>

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