First, arithmetic operators
1, an adder (+)
It represents the addition operation;
Rules for handling special value:
-
- If both operands are strings, then the second operand to the first operand spliced together;
- If only one of the operands is a string, then the other operand is converted to a string, and then stitching together the two strings.
- If there is a target operand, or a Boolean value, which is called the toString () method to obtain the corresponding string value, and then apply the rules on the front of the string.
- For undefined and null , respectively call the String () function and get the string "undefined" and "null" .
- If both operands are strings, then the second operand to the first operand spliced together;
2, subtraction (-)
It represents a subtraction operation;
Rules for handling special value:
-
- If either operand is NaN3 , then the result is NaN3 ;
- If Infinity Save Infinity , the result is NaN3; (Similarly: if -Infinity Save -Infinity , the result is NaN3; )
- If Infinity Save -Infinity , the result is Infinity ; (Similarly: if -Infinity Save Infinity , the result is -Infinity; )
- If either operand is a string, boolean, null or undefined , then the first call in the background Number () function to convert it to a value, and then performs subtraction of the foregoing rules. If the result of the conversion is NaN , then the result of the subtraction is NaN ;
- If either operand is an object, call the object's valueOf () method to obtain a numerical value of the object. If the value obtained is NaN , then the result of the subtraction is NaN . If the object does valueOf () method, which is called toString () String conversion method and the resulting numerical value.
- If either operand is NaN3 , then the result is NaN3 ;
3, multiplication (*)
It represents the product of two numbers;
Processing rules for special values:
-
- If the operands are numbers, performs a conventional multiplication, but if the product is out of range of ECMAScript, returns Infinity or -Infinity ;
- If either operand is NaN3 , then the result is NaN3 ;
- If Infinity and 0 is multiplied, the result is NaN3 ;
- If Infinity NAND 0 is multiplied by the value, the result is Infinity or -Infinity , depending on the number of symbolic representations of operations signed;
- If Infinity and Infinity multiplied, the result is Infinity ;
- If the operand is not a value in the background is called Number () to convert it to a value, then application of the above rules
- If the operands are numbers, performs a conventional multiplication, but if the product is out of range of ECMAScript, returns Infinity or -Infinity ;
4, divide (/)
The second operand represents the first addition operand.
Processing rules for special values:
-
- If the operands are numbers, the conventional division calculation performed, but if outside the range supplier of ECMAScript, returns Infinity or -Infinity ;
- If either operand is NaN3 , then the result is NaN3 ;
- If Infinity is Infinity In addition, the result is NaN3 ;
- If zero is zero, the result is NaN3 ;
- If a nonzero finite number is zero, the result is Infinity or -Infinity , depending on the number of symbolic representations of operations signed;
- If Infinity is in addition to any non-zero value, the result is Infinity or -Infinity , depending on the sign of the signed operands;
- If the operand is not a value in the background is called Number () converts it to the value, and then apply the above rule.
- If the operands are numbers, the conventional division calculation performed, but if outside the range supplier of ECMAScript, returns Infinity or -Infinity ;
5, modulo (%)
The operator indicates modulo number.
Processing rules for special values:
-
- If the operands are numbers, performs division calculation routine returns to the remainder of the divided;
- If the dividend is an infinite value and the divisor is a finite value, the result is NaN3 ;
- If the dividend is a finite value of the divisor is zero, the result is NaN3 ;
- If Infinity is Infinity In addition, the result is NaN3 ;
- If the dividend is a finite value while the divisor is the infinite value, the result is the dividend;
- If the dividend is zero, the result is zero;
- If the operand is not a value in the background is called Number () to convert it to a value, then application of the above rules
- If the operands are numbers, performs division calculation routine returns to the remainder of the divided;
Second, unary operators
1, since the plus (+): itself plus 1
前置++:++变量名;参与运算时,先自身加1,再参与运算。
后置++:变量名++;参与运算时,自身先参与运算,然后再自身加1。
2、自减(- -):自身减1
前置 --:- -变量名;参与运算时,先自身减1,再参与运算;
后置 --:变量名- -;参与运算时,先自身参与运算,然后再自身减1;
注意:操作符对任何值都适用,也就是它们不仅适用于整数,还可以用于字符串、布尔值、浮点数值和对象。在应用于非整数的时候,先把对象的类型转化为整数再进行计算。
三、逻辑运算符
1、逻辑与(&&)
真值表:
第一个操作数 | 第二个操作数 | 结果 |
true | true | true |
true | false | false |
false | true | false |
false | false | false |
逻辑与操作可以应用于任何类型的操作数,而不仅仅是布尔值。在有一个操作数不是布尔值的情况下,逻辑与操作就不一定返回布尔值;此时,它遵循下列规则:
-
- 如果第一个操作数是对象,则返回第二个操作数;
- 如果第二个操作数是对象,则只有在第一个操作数的求值结果为 true 的情况下才会返回该对象;
- 如果两个操作数都是对象,则返回第二个操作数;
- 如果有一个操作数是 null,则返回 null;
- 如果有一个操作数是 NaN,则返回 NaN ;
- 如果有一个操作数是 undefined,则返回 ;
- 如果第一个操作数是对象,则返回第二个操作数;
注意:逻辑与存在短路运算,即如果第一个操作数能够决定结果,那么就不会再对第二个操作数求值。
对于逻辑与操作而言,如果第一个操作数是 false,则无论第二个操作数是什么值,结果都不再可能是true 了。
2、逻辑或(||)
真值表:
第一个操作数 | 第二个操作数 | 结果 |
true | true | true |
true | false | true |
false | true | true |
false | false | false |
与逻辑与操作相似,如果有一个操作数不是布尔值,逻辑或也不一定返回布尔值;此时,它遵循下列规则:
-
- 如果第一个操作数是对象,则返回第一个操作数;
- 如果第一个操作数的求值结果为 false,则返回第二个操作数;
- 如果两个操作数都是对象,则返回第一个操作数'
- 如果两个操作数都是 null,则返回 null;
- 如果两个操作数都是 NaN,则返回 NaN ;
- 如果两个操作数都是 undefined,则返回 undefined;
- 如果第一个操作数是对象,则返回第一个操作数;
注意:与逻辑与操作符相似,逻辑或操作符也是短路操作符。也就是说,如果第一个操作数的求值结果为true,就不会对第二个操作数求值了。
3、逻辑非( ! )
无论这个值是什么数据类型,这个操作符都会返回一个布尔值。
逻辑非操作符首先会将它的操作数转换为一个布尔值,然后再对其求反。
规则:对 true 取反为 false,对 false 取反为 true。
四、关系运算符(比较运算符)
1、相等操作符
相等操作符由两个等于号( ==)表示,如果两个操作数相等,则返回 true。
不相等操作符由叹号后跟等于号( !=)表示,如果两个操作数不相等,则返回 true。
注意:比较的是两个值是否相等。
处理特殊值规则:
-
- 如果一个操作数是字符串,另一个操作数是数值,在比较相等性之前先将字符串转换为数值;
- null 和 undefined 是相等的。
- 要比较相等性之前,不能将 null 和 undefined 转换成其他任何值。
- 如果两个操作数都是对象,则比较它们是不是同一个对象。如果两个操作数都指向同一个对象,则相等操作符返回 true;否则,返回 false。
注意:NaN 与任何都不相等,包括它自身。
2、全等与不全等
全等操作符由3个等号(===)表示,它只在两个操作数未经转换就相等的情况下返回 true ;
不全等操作符由 (!==)表示,它在两个操作数未经转换就不相等的情况下返回 true。
注意:全等和不全等即比较类型也比较值。
Tips: null == undefined 会返回 true,因为它们是类似的值;但 null === undefined 会返回 false,因为它们是不同类型的值。
3、关系操作符
小于(<)、大于(>)、小于等于(<=)和大于等于(>=) 这几个关系运算符对两个值进行比较,都返回一个布尔值。
处理特殊值规则:
-
- 如果两个操作数都是数值,则执行数值比较。
- 如果两个操作数都是字符串,则比较两个字符串对应的字符编码值。
- 如果一个操作数是数值,则将另一个操作数转换为一个数值,然后执行数值比较 。
- 如果一个操作数是对象,则调用这个对象的 valueOf()方法,用得到的结果按照前面的规则执行比较。如果对象没有 valueOf()方法,则调用 toString()方法,并用得到的结果根据前面的规则执行比较。
- 如果一个操作数是布尔值,则先将其转换为数值,然后再执行比较。
- 如果两个操作数都是数值,则执行数值比较。
五、赋值运算符
简单的赋值操作符由等于号( =)表示,其作用就是把右侧的值赋给左侧的变量;
如果在等于号( =)前面再添加乘性操作符、加性操作符或位操作符,就可以完成复合赋值操作。
这种复合赋值操作相当于是对下面常规表达式的简写形式:
Demo:
var num = 10;
num = num + 10; // 等价于 num += 10;
其他的复合赋值操作符还有:*=、/=、%=、+=、-=。
六、条件操作符(三元运算符)
语法格式:
表达式? value1 : value2;
如果表达式值为 true,取 value1的值;如果表达式为 false ,取 value2 的值。(等价于 if...else 语句)
七、运算符的优先级
优先级从高到低:
1、 () 优先级最高
2、 一元运算符 ++ -- !
3、 算数运算符 先* / % 后 + -
4、 关系运算符 > >= < <=
5、 相等运算符 == != === !==
6、 逻辑运算符 先&& 后||
7、 赋值运算符