Blogger Information
Blog 20
fans 0
comment 0
visits 11007
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
js 数据类型 分支循环
Original
368 people have browsed it

1.js数据类型

  1. <script>
  2. // number数值型
  3. let i=10
  4. console.log(typeof i);
  5. // string字符串
  6. let z='10'
  7. console.log(typeof z);
  8. // Boolean布尔类型
  9. let b=false
  10. console.log(typeof b);
  11. // undefined 定义不赋值
  12. let d
  13. console.log(typeof d);
  14. // NULL空类型
  15. let a=null
  16. // array数组
  17. let c=[1,2,3,4,5]
  18. console.log(c[3]);
  19. // object对象
  20. let e={name:1,pass:2};
  21. console.log(e.name);
  22. // function函数
  23. (function(){
  24. console.log(666);
  25. }())
  26. </script>

2.流程控制分支

  1. 单分支
  2. if(条件为真){执行代码}
  3. //双分支
  4. if(条件为真){执行代码}else{否则执行代码2}
  5. //if多分支
  6. if(条件为真){
  7. 代码1
  8. }else if(条件2){
  9. 代码2
  10. }else{
  11. 代码3
  12. }
  13. //switch单值判断
  14. switch(true){
  15. case 条件1:
  16. 代码1
  17. break
  18. case 条件2:
  19. 代码2
  20. break
  21. default:
  22. 都不满足代码3
  23. }
  24. 三元表达式
  25. 条件?真执行1:假执行2

  1. // 单分支
  2. if(true){
  3. console.log('test')
  4. }
  5. // 双分支
  6. let i=1
  7. if(i===3){
  8. console.log('test1')
  9. }else{
  10. console.log('test2')
  11. }
  12. // 多分支
  13. if(i===2){
  14. console.log('test3')
  15. }else if(i===3){
  16. console.log('test4')
  17. }else{
  18. console.log('test5')
  19. }
  20. // switch判断
  21. let z=1
  22. switch(z){
  23. case 2:
  24. console.log(666);
  25. break
  26. case 3:
  27. console.log(66);
  28. break
  29. default:
  30. console.log(6666);
  31. }
  32. // 三元表达式
  33. console.log(1>2?'是的':'不是')

3.js循环

  1. while循环 while(条件表达式){执行语句}
  2. do while循环 do{执行语句}while(条件表达式)
  3. for循环 for(初始话变量;条件表达式;自增或自减){执行语句}
  4. for-of for(定义变量 of 需要遍历的数组){执行语句}
  5. for-in for(定义变量 in 需要遍历的对象或数组){执行语句}
  1. <script>
  2. let a=[1,2,3,4,5]
  3. let i=0
  4. while(i<a.length){
  5. console.log(a[i]);
  6. i++
  7. }
  8. console.log('------------------');
  9. let z=0
  10. do{
  11. console.log(a[z])
  12. z++
  13. }while(z<a.length)
  14. console.log('------------------');
  15. a=[6,7,8,9]
  16. for(let i=0;i<a.length;i++){
  17. console.log(a[i]);
  18. }
  19. console.log('------------------');
  20. // for-of
  21. // 遍历值
  22. for(let i of a){
  23. console.log(i)
  24. }
  25. console.log('------------------');
  26. // 遍历索引
  27. for(let i of a.keys()){
  28. console.log(i)
  29. }
  30. // 遍历所有和值
  31. console.log('------------------');
  32. for(let i of a.entries()){
  33. console.log(i)
  34. }
  35. console.log('------------------');
  36. // for in 遍历对象
  37. a={name:1,pass:2}
  38. for(let i in a){
  39. // 遍历索引
  40. console.log(i)
  41. // 遍历值
  42. console.log(a[i]);
  43. }
  44. </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