Blogger Information
Blog 15
fans 0
comment 0
visits 10881
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Javascript常用的数据类型及流程控制
P粉932932019
Original
486 people have browsed it

一、数据类型

1、原始类型

  • num
  • string
  • boolean
  • undefined
  • null

①、数据类型检查:console.log(type of 数据)
②、Null返回是object类型,并非是 Null
③、let a; // undefined是变量的默认值

2、引用类型

  • array
  • function
  • object

二、流程控制

1、单分支

  1. let age=18;
  2. if (age>=18) {
  3. console.log('成年人')
  4. };

2、双分支

  1. if (age >= 18) {
  2. console.log("成年人");
  3. } else {
  4. console.log("未成年人"); //默认值
  5. };
  1. age > 18 ? console.log("成年人") : console.log("未成年"); //语法糖:三元运算符,

3、多分支

  1. if (age >= 1 && age < 18) {
  2. console.log("未成年人");
  3. } else if (age >= 18 && age <= 30) {
  4. console.log("青年人");
  5. } else if (age > 30 && age <= 50) {
  6. console.log("中年人");
  7. } else if (age > 50 && age < 100) {
  8. console.log("老年人");
  9. } else if (age < 1 || age > 100) {
  10. console.log("不合法的年龄");
  11. }
  • && 是并列and的意思
  • || 是或者or的意思
  1. switch (true) {
  2. case age >= 1 && age < 18:
  3. console.log("未成年人1");
  4. break;
  5. case age >= 18 && age <= 30:
  6. console.log("青年人1");
  7. break;
  8. case age > 30 && age <= 50:
  9. console.log("中年人1");
  10. break;
  11. case age > 50 && age < 100:
  12. console.log("老年人1");
  13. break;
  14. default:
  15. console.log("不合法年龄");
  16. }
  • 区间判断,并非单值,一定用true才能进入分支
  • 必须要用true做为switch入口判断条件
  • 每一条区间判断结束必须添加break跳出,否则继续执行
  • default是不在以上条件之外,即默认值

4、循环

1、数组循环
while

  1. let names = ["Zhang", "Li", "Wang", "Chen"];
  2. let length = names.length;
  3. let i = 0;
  4. while (i < length) {
  5. console.log(names[i]);
  6. i++;
  7. }

while循环必须有初始化值、条件、增量

  1. i = 0;
  2. do {
  3. console.log(names[i]);
  4. i++;
  5. } while (i < length);

for

  1. for (i = 0; i < length; i++) {
  2. console.log(names[i]);
  3. }

for-of

  1. for (let item of names) {
  2. console.log(item);
  3. }
  1. for (let item of names.values()) {
  2. console.log(item);
  3. }
  1. for (let item of names.keys()) {
  2. console.log(item);
  3. }
  1. for (let item of names.entries()) {
  2. console.log(...item);
  3. }

for-in

  1. for (let item in names) {
  2. console.log(names[item]);
  3. }

2、对象循环
for-in

  1. let obj = {
  2. name: "Zhang",
  3. sex: "Male",
  4. age: 50,
  5. detail: function () {},
  6. };
  7. for (let item in obj) {
  8. console.log(obj[item]);
  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