1. Univariate operator
1. Self-increase and self-decrease operators: divided into front-end and rear-end;
Pre-type: ++a;--a;
Post-type: a++; a--;
example:
The code copy is as follows:
<script type="text/javascript">
var a, b,i= 1,j=1;
a=i++;
b=++j;
alert("a="+a+",i="+i+",b="+b+",j="+j);//a=1,i=2,b=2,j=2
</script>
where a=i++ is equivalent to a=i;i=i+1;
And b=++j, equivalent to j=j+1;b=j;
2. Unary addition and subtraction operators: a=+i;a=-i;
The code copy is as follows:
<script type="text/javascript">
var a, b,i= 1,j=1;
a=+i;
b=-j;
alert("a="+a+",i="+i+",b="+b+",j="+j);//a=1,i=1,b=-1,j=1
</script>
For integers, a one-digit subtraction is equivalent to taking a negative number.
2. Bit operator
1. Bitwise non-~ (NOT)
No, that is, inverse all numbers in binary form.
Common usage: bit operation NOT is essentially a negative number and then subtract 1
2. Bitwise or | (OR)
Or, that is, all numbers and target numbers are bitwise or operated in binary form.
Common usage: It is usually used for unconditional assignment of binary numbers. For example: a number |1 is equivalent to the odd number closest to the current number.
3. Bitwise & & (AND)
And, that is, all numbers and target numbers are bitwise in binary form.
Common usage: It is usually used for binary bit-taking operations, such as: a number &1, if the result is 0, it is an even number, and if it is 1, it is an odd number.
4. Bitwise XOR^ (XOR)
Exclusive OR means that all numbers are compared with the target number in binary form. Only two numbers are different, that is, only one digit stores 1 when 1 is 1, and return 0 if two numbers are the same.
Common usage: The inverse operation of xor operation is itself, that is, the final result of the xor number is unchanged twice. Can be used for simple encryption, or interactive numerical operations.
5. Move left<<
Move left, that is, move all numbers to the left in binary form, move the corresponding number of digits, move the high position out (discard), and fill the zero position in the low position. Moving left will not affect the sign bit.
Mathematical significance: On the premise that numbers do not overflow, for positive and negative numbers, shifting left one is equivalent to multiplying by 2 to the power of 1, and shifting left n bits is equivalent to multiplying by 2 to the power of n.
6. Move right
6.1 Signed right shift>>: that is, move all values to the right in binary form but retain the sign bits.
Mathematical significance: On the premise that numbers do not overflow, for positive and negative numbers, shifting right one is equivalent to dividing by 2 to the first power, and shifting right n bits is equivalent to dividing by 2 to the n power.
6.2 Unsigned right shift>>>: that is, move all values, including sign bits, to the right in binary form.
For positive numbers, the results are the same;
For negative numbers, since negative numbers are represented in their absolute value complement, the result will be very large when unsigned right shifts.
3. Boolean operator
1. Logical non-logic!
Logic is not useful! Indicates that any type of value with ECMAScript can be applied, and the logical non-operation returns a boolean value (true/false). This operator first converts its operand to a Boolean value and then inverses it.
! ! Equivalent to the Boolean() function.
A set of rules for Boolean() transformation function.
The value converted to true is converted to false
Booleanttruefalse
String Any non-empty string "" (empty string)
Number Any non-zero numeric value (including infinity) 0 and NaN
Object any object null
Undefined No undefined
2. Logic and &&
There are two operands for logic and there are two operands.
Logic and operations can be applied to any type of operand, not just booleans. When an operand is not a Boolean, logic and operations do not necessarily return a Boolean; at this time, it follows the following rules:
1. If the first operand is an object, the second operand is returned;
2. If the second operand is an object, the object will only be returned if the evaluation result of the first operand is true;
3. If both operators are objects, the second operand is returned; follow the first rule.
4. If an operand is null, return null;
5. If an operand is NaN, return NaN;
6. If an operand is undefined, undefined is returned.
Logic and operations are short-circuit operations, that is, if the first operand can determine the result, then the second operand will not be evaluated again. (It can be understood as two internal return operations). Therefore, when the rules 4, 5, and 6 conflict, the short-circuit operation principle is followed.
The code copy is as follows:
var nul = null;
var na = NaN;
var test;
test = na&&nul;
document.write(test); //NaN
The code copy is as follows:
var nul = null;
var na = NaN;
var test;
test = nul&&na;
document.write(test); //null
3. Logical or ||
Logical or has two operands.
Logical or similar to logic, operations can be applied to any type of operand, not just booleans. In the case where an operand is not a Boolean, logic or operation does not necessarily return a Boolean; at this time, it follows the following rules:
1. If the first operand is an object, the first operand is returned;
2. If the result of the first operand is false, the second operand is returned;
3. If both operators are objects, the first operand is returned, following the first rule.
4. If both operands are null, return null;
5. If both operands are NaN, return NaN;
6. If both operands are undefined, undefined is returned.
Logic or operation is a short-circuit operation, that is, if the first operand result is true, then the second operand will not be evaluated again.
We can use this property of logic or to avoid assigning null or undefined values to variables
For example: var myObject=firstObject||secondObject
If firstObject is not null, firstObject is assigned to myObject, otherwise the value of secondObject is assigned to myObject.
The above content is all about this article, I hope it will be helpful to everyone