JS manipulation statements: determining, loop
Condition holds what? Not established what?
- if /else if/else
if(条件){
条件成立执行
} else if(条件2){
条件2成立执行
} ....
else{
以上条件都不成立
}
- Ternary operator
条件? 条件成立事件 : 不成立处理的事情
1.如果处理的事情比较多,我们用括号抱起来,每一件事情用逗号分隔,
2.如果不需要处理事情,可以使用 null/undefined 站位
let a = 10;
a > 0 && a < 20 ? (a++ , console.log(a)) : null;
if (a > 0 && a < 20) {
a++;
console.log(a);
}
-----------------------------------------------------------
a > 0 ? (a < 10 ? a++ : a--) : (a > -10 ? a += 2 : null);
/*
if (a > 0) {
if (a < 10) {
a++;
} else {
a--;
}
} else {
if (a > -10) {
a += 2;
}
} */
- switch case
不加break,当前条件成立执行完成后,后面条件不论是否成立都要执行,直到遇到break为止(不加beak 可以实现变量在某些值的情况下做相同的事情)
switch 里面的case 情况的比较用的都是 '===' 绝对相等
if里面情况比较用的的是 '==' 相等
let a = 1;
switch (a) {
case 1: console.log("哈哈");
case 5: console.log('嘿嘿5');
break;
case 9: console.log('滴滴9');
break;
case 10: console.log('嘻嘻10');
break;
default: console.log('哦豁');//以上都不成立
}
#### === vs ==
== Equal to: if the data value is not the same type, certainly not different types, the default is first converted to the same type, and then compare '5' == 5 => true
=== absolutely equal: If the type is not the same, certainly not equal, the default will not convert the data type '5' === 5 => false
Project in order to ensure that the business cautiously recommended ===;
cycle
Repeat the cycle is to do certain things
continue: the end of the current round loop (continue behind the code is not executed) proceed to the next round
break: the forced end of the whole cycle (after the code execution is BREAK) and the whole cycle is ended what is not dry
for loop
/* 1.创建循环初始值 2.设置 循环执行的条件 3.条件成立执行循环体中的内容 4.当前循环结束执行步长累计操作 */
for in loop
for of loop
while loop
do while loop
Is equal to: If the left and right sides of the different types of data values, the default is first converted to the same type, then comparing the