1. Comparison operations
- In js, every operation will have a return value
- >, <, ==, !=, >=, <=, ===, !==. The return value of the comparison operation is true/false, which is a Boolean value.
- During a comparison operation, if you determine whether the values of two variables are equal, there
==
will be an implicit conversion of the data type. js is a weakly typed language, and data types will be converted to each other. ===
The execution efficiency is higher than that==
because the latter requires data type conversion.
console.log(a = 2);//2 赋值操作运算符,返回结果是等号右边的值。
var a = 4,
b = 10,
c = 4,
d = "4";
console.log(a = "你好");//你好
console.log(a > b);//false
console.log(a == b);//false
console.log(a < b);//true
console.log(a != b);//true
console.log(a == d);//true
console.log(a === d);//false 不仅值要相等,数据类型也需要相等
2. if statement
if(条件){
条件正确的执行代码
}else{
否则执行的代码
}
- In () of if, implicit type conversion will occur.
- Only in the following six cases, the data in () in if is false.
false
,空字符串
,0
,null
,undefined
,NaN
.
3. Ternary operation
- Has a condition, a true result, and a false result.
- Ternary arithmetic: condition? true statement: false statement. takes precedence over assignment
oBox.title = oBox.title == "a"?"b":"a";
4. switch statement
- The judgment condition of using switch to replace if is that when multiple certain values are compared, the judgment in switch is that they are all equal.
- Add a break after each case to exit the switch loop.
switch(a){
case 1:
console.log(1);
break;
case 2:
console.log(2);
break;
case 3:
console.log(3);
break;
default:
console.log("其他");
break;
}
5. Things to note when making judgments
- Don't use color to make judgments.
- Do not use composite attributes to make judgments.
- Do not use URL to make judgments, which involves string conversion and relative/absolute paths.