This article focuses on the JavaScript operators, including a unary operator, bitwise operator, Boolean operators of these 3 types, explained a very comprehensive, small partners under the reference
-->
One or one-dollar operator
1, self-reduction operator: divided into the front and rear-type;
Front type: ++a;--a;
Rear-mounted type: a++;a--;
Cases:
The code 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>
Among them a=i++, equivalent to a=i;i=i+1;
And B=++j, the equivalent of j=j+1;b=j;
2, one-dollar plus and minus operators: a=+i;a=-i;
The code 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 unary subtraction is equivalent to taking a negative number.
Second, bitwise operator
1, bitwise NON ~ (not)
Not, that is, all numbers are reversed in binary form.
Common usage: The bitwise operand is essentially negative for the number and then minus 1
2, by bit or | (OR)
Or, that is, all numbers and the target digits are bitwise OR manipulated in binary form.
Common usage: An unconditional assignment that is typically used for binary numbers. For example: A digital |1 is equivalent to getting the odd number closest to the current digit.
3, Bitwise AND & (and)
And, that is, in binary form, all the numbers and the target number are bitwise and operate.
Common usage: Typically used for binary bit-taking operations, such as a numeric &1, or even if the result is 0, or an odd number if 1.
4, bitwise exclusive OR ^ (XOR)
Different or, that is, in binary form to compare all numbers to the target number, only two numbers are different, that is, only one digit is 1 of the time to return 1, such as two digits the same return 0.
Common usage: The inverse of the XOR operation is itself, that is, two times or the same number, the final result is unchanged. Can be used for simple encryption, or for interacting with numeric operations.
5, left-shift <<
Left, that is, in binary form all the digits to the left to move the corresponding digits, high move out (discard), low vacancy of 0. Moving left does not affect the sign bit.
Mathematical significance: In the absence of overflow, the number of positive and negative, the left one is equal to multiply 2 of the 1 times, left n bit is equivalent to times 2 of N.
6. Move Right
6.1 Signed right Shift >>: that is, all values are moved to the right in binary form, but the sign bit is preserved.
Mathematical significance: In the absence of overflow, for both positive and negative numbers, the right one is equal to 1 times divided by 2, and the right n bit is the equivalent of dividing by 2 of N.
6.2 Unsigned Right Shift >>>: that is, all values, including symbol bits, are moved to the right in binary form.
The results of >> and >>> are the same for positive numbers;
For negative numbers, negative numbers are represented as the complement of their absolute values, which results in a very large, unsigned right shift.
Three, Boolean operators
1, logic is not!
Logic is not used! Indicates that a value of any type can be applied to the ECMAScript, and the logical non operation returns a Boolean value (True/false). The operator first converts its operand to a Boolean value, and then it is reversed.
!! Equivalent to the Boolean () function.
A set of rules for a Boolean () transition function.
Value with value converted to true for data type conversion to False
Boolean true False
String any non-empty string "" (empty string)
Number any non 0 numeric value (including infinity) 0 and Nan
Object any objects Null
Undefined No Undefined
2. Logic and &&
Logic with two operands.
Logic and operations can be applied to any type of operand, not just a Boolean value. In the case where an operand is not a Boolean, the logic and operation do not necessarily return a Boolean value; At this point, it follows the rules:
1. Returns the second operand if the first operand is an object;
2. If the second operand is an object, the object is returned only if the first operand evaluates to true;
3. If the two operators are objects, return the second operand, followed by the first rule.
4. Return NULL if one operand is null;
5. Returns nan if one operand is nan;
6. If one operand is undefined, return undefined.
Logic and operation are short-circuit operations, that is, if the first operand can determine the result, then the second operand will not be evaluated. (Can be understood as the internal two return operations). Therefore, when the 4, 5, 6 rules conflict, follow the short-circuit operation principle.
The code is as follows:
var nul = null;
var na = NaN;
var test;
Test = na&&nul;
document.write (test); NaN
The code is as follows:
var nul = null;
var na = NaN;
var test;
Test = nul&&na;
document.write (test); Null
3. Logic or | |
Logical OR has two operands.
Logical or logical and similar, operations can be applied to any type of operand, not just a Boolean value. In the case where an operand is not a Boolean, the logic or operation does not necessarily return a Boolean value; At this point, it follows the rules:
1. Returns the first operand if the first operand is an object;
2. Returns the second operand if the result of the first operand is false;
3. If the two operators are objects, the first operand is returned, followed by the first rule.
4. Returns null if the two operands are null;
5. Return Nan If the two operands are Nan;
6. If two operands are undefined, return undefined.
Logic or operation is a short-circuit operation, that is, if the first operand results in true, then the second operand is not evaluated.
We can use the logic or this feature to avoid assigning null or undefined values to a variable.
For example: var myobject=firstobject| | Secondobject
If Firstobject is not NULL, the Firstobject is assigned to MyObject, otherwise the Secondobject value is assigned to MyObject.
The above content is the whole of this article, hope to be helpful to everybody