Blogger Information
Blog 14
fans 2
comment 2
visits 6638
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Js常用数据类型与分支循环实例演示
西门瑶雪
Original
478 people have browsed it

1. 实例演示常用数据类型,特别是引用类型

数据类型

number:数值
sting:字符串
bool:布尔值/是否
undefined:未定义
null:空值

  1. //数据类型演示
  2. <script>
  3. console.log(100);
  4. console.log(typeof 100);
  5. console.log("100PHP");
  6. console.log(typeof "100PHP");
  7. console.log(typeof true);
  8. let ad;
  9. console.log(ad);
  10. console.log(typeof null);
  11. </script>

  1. //数组:使用常量引用,字符串必须加引号;数组必须用[]方括号声明
  2. const srr=["电脑",88,8000]
  3. console.log(srr);
  4. console.table(srr);
  5. console.log(srr[0]);
  6. console.log(srr[2]);
  7. console.log(srr[1]);
  8. //对象:使用常量引用,字符串必须加引号;对象必须用大括号声明{}
  9. const syy={
  10. name:"电脑",
  11. num:88,
  12. prc: 8000,
  13. "my tel":19900990088,}
  14. console.log(syy["name"])
  15. console.log(syy["prc"])
  16. console.log(syy.num);
  17. console.log(syy["my tel"]);
  18. //附加函数和this写法
  19. const sii = {
  20. name:"电脑",
  21. num:88,
  22. prc: 8000,
  23. "my tel":19900990088,
  24. tat:function () {
  25. return sii.name + "总价:" + sii.num*sii.prc + "英镑";
  26. }}
  27. console.log(sii.tat());
  28. </script>

  1. <script>
  2. //this便捷应用写法
  3. const siiyy = {
  4. name:"电脑",
  5. num:88,
  6. prc: 8000,
  7. "my tel":19900990088,
  8. tat:function () {
  9. return this.name + "总价:" + this.num*this.prc + "英镑";
  10. }}
  11. console.log(siiyy.tat());
  12. </script>

2. 实例演示所有分支与循环类型

循环类型

if-else:演示单分支,双分支,多分支,循环
switch:(case break default),case后切记为:冒号
while:(索引值,判断条件,更新:初始条件,判断条件,更新条件C++)
for-of:循环
for-in:循环

  1. //分支是有条件执行的代码
  2. //单分支=一条语句仅有一条结果即为单分支
  3. let agg = 29;
  4. console.log(agg >= 12);
  5. if (agg >= 12) {
  6. console.log("允许查看射雕英雄传第一集");
  7. }
  8. //双分支=有2条结果即为双分支
  9. console.log(agg >= 32);
  10. if (agg >= 32) {
  11. console.log("允许查看射雕英雄传第一集");
  12. }
  13. else {
  14. console.log("当前会员等级仅允许观看奥特曼!");
  15. }
  16. //多分支=有多条结果即为多分支
  17. agg = 180;
  18. console.log(agg >= 32 && agg < 40);
  19. if (agg >= 32) {
  20. console.log("允许查看射雕英雄传第一集");
  21. }
  22. console.log(agg >= 40 && agg < 50);
  23. if (agg >= 32) {
  24. console.log("允许查看射雕英雄传第18集");
  25. }
  26. console.log(agg >= 50 && agg < 100);
  27. if (agg >= 32) {
  28. console.log("允许查看射雕英雄传第55一集");
  29. }
  30. console.log(agg >= 150 || agg < 10);
  31. if (agg >= 180) {
  32. console.log("非法会员,未经授权非法访问,你已被通缉,请立即归案!");
  33. }
  34. else {
  35. console.log("当前会员等级仅允许观看奥特曼!");
  36. }

  1. //switch演示多分支--简化
  2. console.log("switch演示多分支函数条件必须默认为true,条件后务必使用");
  3. //switch演示多分支,函数条件必须默认为true,条件后务必使用:
  4. const ssg = -12;
  5. switch (true) {
  6. case ssg>=10 && ssg < 20:
  7. console.log("允许查看射雕英雄传第1集");
  8. break;
  9. case ssg>=20 && ssg < 30:
  10. console.log("允许查看射雕英雄传第10集");
  11. break;
  12. case ssg>=30 && ssg < 40:
  13. console.log("允许查看射雕英雄传第15集");
  14. break;
  15. case ssg>=70 || ssg < 5:
  16. console.log("非法会员,未经授权非法访问,你已被通缉,请立即归案!");
  17. break;
  18. default:
  19. console.log("当前会员已失效或重新再试!");
  20. }
  1. //if-else循环
  2. const see = ["电脑",2,8080]
  3. console.log(see);
  4. console.log(see[0]);
  5. console.log(see[2]);
  6. console.log(see[1]);
  7. console.log(see.length);
  8. console.log(see[see.length-1]);
  9. console.log(see[see.length-2]);
  10. console.log(see[see.length]);
  11. let t=0;//声明一个变量等于索引值
  12. let length = see.length;//声明两个参数相等,目的是绑定关系;
  13. if (t < length) {
  14. console.log(see[t]);
  15. t = t + 1; //可简化为 t++;
  16. }
  17. if (t < length) {
  18. console.log(see[t]);
  19. t = t + 1; //可简化为 t++;
  20. }
  21. if (t < length) {
  22. console.log(see[t]);
  23. t = t + 1; //可简化为 t++;
  24. }
  25. if (t < length) {
  26. console.log(see[t]);
  27. t = t + 1; //可简化为 t++;
  28. }
  29. else {
  30. console.log("数据已查询完成!");
  31. }
  1. console.log("***********");
  2. //while循环:变量索引值,条件判断;
  3. t=0;
  4. while (t < length) {
  5. console.log(see[t]);
  6. t++;
  7. }
  8. //for可以当成while的语法糖
  9. console.log("+++++++++++++++");
  10. for (let t=0 ; t < length ; t++) {
  11. console.log(see[t]);
  12. }
  1. //for of 循环
  2. console.log("++++++++");
  3. for (let item of see){
  4. console.log(item);
  5. }
  1. //查看参数的值
  2. console.log("++++++++");
  3. for (let item of see.values()){
  4. console.log(item);
  5. }
  6. //查看参数的健(索引值序号)
  7. console.log("++++++++");
  8. for (let item of see.keys()){
  9. console.log(item);
  10. }
  11. //查看参数的值和健(索引值序号)
  12. console.log("++++++++");
  13. for (let item of see.entries()){
  14. console.log(item);
  15. }

  1. //对象的索引 for in
  2. const sdd={
  3. name:"电脑",
  4. prc:5880,
  5. num:100099999
  6. }
  7. for (let as in sdd){
  8. console.log(sdd[as]);
  9. }
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