Correcting teacher:PHPz
Correction status:qualified
Teacher's comments:
1、原始类型
①、数据类型检查:console.log(type of 数据)
②、Null返回是object类型,并非是 Null
③、let a; // undefined是变量的默认值
2、引用类型
1、单分支
let age=18;
if (age>=18) {
console.log('成年人')
};
2、双分支
if (age >= 18) {
console.log("成年人");
} else {
console.log("未成年人"); //默认值
};
age > 18 ? console.log("成年人") : console.log("未成年"); //语法糖:三元运算符,
3、多分支
if (age >= 1 && age < 18) {
console.log("未成年人");
} else if (age >= 18 && age <= 30) {
console.log("青年人");
} else if (age > 30 && age <= 50) {
console.log("中年人");
} else if (age > 50 && age < 100) {
console.log("老年人");
} else if (age < 1 || age > 100) {
console.log("不合法的年龄");
}
- && 是并列and的意思
- || 是或者or的意思
switch (true) {
case age >= 1 && age < 18:
console.log("未成年人1");
break;
case age >= 18 && age <= 30:
console.log("青年人1");
break;
case age > 30 && age <= 50:
console.log("中年人1");
break;
case age > 50 && age < 100:
console.log("老年人1");
break;
default:
console.log("不合法年龄");
}
- 区间判断,并非单值,一定用true才能进入分支
- 必须要用true做为switch入口判断条件
- 每一条区间判断结束必须添加break跳出,否则继续执行
- default是不在以上条件之外,即默认值
4、循环
1、数组循环
while
let names = ["Zhang", "Li", "Wang", "Chen"];
let length = names.length;
let i = 0;
while (i < length) {
console.log(names[i]);
i++;
}
while循环必须有初始化值、条件、增量
i = 0;
do {
console.log(names[i]);
i++;
} while (i < length);
for
for (i = 0; i < length; i++) {
console.log(names[i]);
}
for-of
for (let item of names) {
console.log(item);
}
for (let item of names.values()) {
console.log(item);
}
for (let item of names.keys()) {
console.log(item);
}
for (let item of names.entries()) {
console.log(...item);
}
for-in
for (let item in names) {
console.log(names[item]);
}
2、对象循环
for-in
let obj = {
name: "Zhang",
sex: "Male",
age: 50,
detail: function () {},
};
for (let item in obj) {
console.log(obj[item]);
}