Home > Article > Web Front-end > Comparison of js operator precedence and analysis of js logical operators
This article provides a detailed explanation of different categories of operators, and then describes the priority of js operators. The priority refers to the calculated priority level of the operator. It will be introduced in detail below.
How to compare operators in js? Comparison operators in the JavaScript language are mainly used to compare whether the values of two operands are equal or the size. The article has reference value and can be collected if needed.
JavaScript provides a set of operators for operating data values, also known as operators
Operators can be operated according to different functions or the number of operating variables. Category
Arithmetic operators
1. If one or both of the operands are string types, JavaScript will automatically convert them to numeric values. When performing calculations
2. If one or two of the operands are of string type but the characters in them are not numbers, JavaScript will automatically convert the numeric value to a NaN result
3. Any operand is a NaN result. Both are NaN
4. Boolean values false and true will be converted to 0 and 1 for calculation
Addition operator
2. If strings are added, string concatenation operator (string string)
3. If Boolean type is added, Boolean will be added. Convert the type to a numeric value and then perform addition calculation
2. If the string is subtracted - convert the string type to a numeric value and then perform the subtraction
3. If the Boolean type is subtracted - convert the Boolean type to a numeric value and then perform the subtraction Calculation
Result Whether a positive number or a negative number is related to whether the first operand is positive or negative (it has nothing to do with the second one)
console.log(100 % 3);//1 console.log(8 % 4);//0 console.log(100 % -3);//1 console.log(8 % 4);//0 console.log(-100 % 3);//-1 console.log(-8 % 4);//0 console.log(-100 % -3);//-1 //与减法的情况保持一致 console.log('卧龙学苑' % 2);//NaN
a. Preposition type: The auto-increment operator is placed before the operand and adds 1 before assigning the value
前置自增运算符 - ++变量名b. Post-increment type: the auto-increment operator is located after the operand, first assigns the value and then adds 1
后置自增运算符 - 变量名++The auto-decrement operator is used for integer values -1 successively and is divided into:
a. Pre-position type: auto-decrement operator Before the operand, decrement it by 1 and then assign it
b. Postposition type: the auto-decrement operator is before the operand, assign it and then decrement it by 1
operator The precedence level Operators have calculated precedence levels
1. Operators with higher precedence levels are calculated first
2. Operators have the same level and are calculated from left to right
3. The operator with the highest priority is "()"
4. The expression that is evaluated first is wrapped with "()"
console.log(100 + 200 - 150 * 3);// -150 console.log(100 + 200 % 3);// 102 console.log(2 * 200 % 3);// 1 var num = 10; console.log(5 + ++num);// 16
Comparison operators
一.大于与小于比较运算符
console.log(10 > 11);//false console.log(11 > 10);//true console.log(10 >= 10);//true // 2.boolean类型 - 将boolean类型转换为number类型 console.log(true > false);//true console.log(true > 0);//true /* 3.string类型 - a.英文; b.中文 * 英文或中文 - 将文本转换成Unicode码 - 对应具有数字值 * 英文单词 - 从左至右的一次比较字母 Unicode 码的大小 */ console.log('a' < 'b');//true console.log('a' > 'A');//true console.log('abc' > 'cba');//false console.log('abc' > 'acd');//false console.log('我' > '你');//true
二.相等与不等比较运算符Equality comparison operator
*The difference between assignment operator
*Assignment operator (=)
*Equality comparison operator (==)
Inequality comparison operator
*The symbol is "!="
*Not "a8093152e673feb7aba1828c43532094"
Equality and inequality comparison operators only compare the values of the operands and do not compare types
// 1.number类型 console.log(10 == 10);// true console.log(10 == 11);// false // 2.boolean类型 console.log(true == true);// true console.log(true == false);// false console.log(true == 1);// true // 3.string类型 console.log('a' == 'a');// true console.log('a' == 'b');// false console.log('100' == 100);// true
三.全等与不全等运算符 全等与不全等运算符 不仅比较值 还比较类型
console.log(10 === 10);// true console.log('10' === 10);// false console.log('10' == 10);// true
Function: determine whether the current value is NaN
true - means The current value is NaN (not a numeric value)
false - Indicates that the current value is not NaN (not a numeric value)
isFinite() function
Function - Determines whether the current value is infinity
false - Indicates The current value is infinity
true - indicates that the current value is a finite value
console.log(isNaN(100));// false console.log(isNaN(Number('卧龙学苑')));// true var result = 100/0; console.log(result);// Infinity //isFinite()函数 console.log(isFinite(result));// false
When using the logical AND operator, if both operands are of Boolean type, the result returned will be true only when both operands are true, otherwise they will be false
1. The two operands of the logical AND operator are converted to Boolean type
2. When the left operand is true, the result is the value of the right operand
3. When the left operand is false, the result is the left operation The value of the number
console.log(true && true);// true console.log(true && false);// false console.log(false && true);// false console.log(false && false);// false console.log(100 && 1);// 1 console.log(1 && 0);// 0 console.log(0 && 1);// 0 console.log(0 && 0);// 0 console.log(true && true); console.log(true && false); console.log(false && true); console.log(false && false); console.log('卧龙学苑' && '卧龙学苑');// 卧龙学苑 console.log('卧龙学苑' && '');// '' console.log('' && '卧龙学苑');// '' console.log('' && '');// '' console.log('卧龙学苑' && 1);// 1 console.log(false && 0);// false
console.log(true || true);// true console.log(true || false);// true console.log(false || true);// true console.log(false || false);// false console.log(100 || 1);// 100 console.log(1 || 0);// 1 console.log(0 || 1);// 1 console.log(0 || 0);// 0 console.log(true || true); console.log(true || false); console.log(false || true); console.log(false || false); console.log('卧龙学苑' || '卧龙学苑');// 卧龙学苑 console.log('卧龙学苑' || '');// 卧龙学苑 console.log('' || '卧龙学苑');// 卧龙学苑 console.log('' || '');// ''
The assignment expansion operator in JavaScript language is actually the abbreviation mode of assignment operator and arithmetic operator.
Assignment expansion operator in JavaScript language The execution efficiency is higher
var b = 10, c = 20; var a = b + c; console.log(a);// 30 // b = b + c; // 等价写法 - 赋值扩展运算符的性能要比赋值运算符更高 b += c; console.log(b);// 30
条件运算符
条件运算符 首先判断一个表达式是真是假 然后根据判断结果执行两个给指定指令中的一个
var result = 8 > 9 ? '对' : '错'; console.log(result); var score = 95; score > 90 ? console.log('优秀') : console.log('及格');
嵌套条件运算符
条件运算符中 每个表达式可以又是一个条件运算表达式 称为条件运算的嵌套
优点:扩展了条件运算符本身的计算能力
缺点:可读性比较差 性能随着嵌套的层级越多越差
建议:不要最多不要超过三层嵌套
var score = 55; var result = score > 90 ? '优秀' : (score > 80 ? '良好' : (score > 60 ? '及格' : '不及格')); console.log(result);
相关推荐:
The above is the detailed content of Comparison of js operator precedence and analysis of js logical operators. For more information, please follow other related articles on the PHP Chinese website!