Learn JavaScript from the beginning (iv)--operator

Source: Internet
Author: User
Tags bitwise operators

Original: Learn JavaScript from the beginning (iv)--operator

One or one-dollar operator

1. Self-increment self-subtraction operator: divided into pre-and post -type;

Front type: ++a;--a;

Post-type: a++;a--;

Cases:

1   <script type= "Text/javascript" >2         var A, b,i= 1,j=1; 3         a=i++; 4         b=++J; 5         Alert ("A=" +a+ ", i=" +i+ ", b=" +b+ ", j=" +j);//a=1,i=2,b=2,j=26     </script>
Among them a=i++, equivalent to a=i;i=i+1;
And B=++j, the equivalent of j=j+1;b=j;
2, unary plus minus operator: a=+i;a=-i;
1  <script type= "Text/javascript" >2         var A, b,i= 1,j=1; 3         a=+i; 4         b=-J; 5         Alert ("A=" +a+ ", i=" +i+ ", b=" +b+ ", j=" +j);//A=1,i=1,b=-1,j=1

6 </script>

For integers, a unary minus equals a negative number.

Second, bitwise operators

1, bitwise NON ~ (not)

Non, which is to reverse all numbers in binary form.

common usage: bit arithmetic is not essentially negative for numbers, then minus 1

2. Bitwise OR | (OR)

Or, in binary form, all numbers and target digits are bitwise OR manipulated.

common usage: typically used for unconditional assignment of binary numbers. For example: A number,-|, equivalent to getting the odd number closest to the current digit.

3, Bitwise AND & (and)

With, that is, in binary form, all numbers and target digits are bitwise and manipulated.

common usage: A bitwise operation commonly used for binary, for example: A number &1, if the result is 0 is even, and if 1 is odd.

4, Bitwise XOR or ^ (XOR)

XOR, that is, in binary form all the numbers compared to the target number, only two numbers are different that only one digit is stored 1 when the time to return 1, such as two digits the same return 0.

common usage:the inverse of an XOR operation is itself, that is, two times or the same number and the final result is unchanged. Can be used for simple encryption, or interactive numeric manipulation.

5. Shift left <<

Shift left, that is, in binary form all the numbers to the left to move the corresponding number of digits, high-level emigration (discard), low vacancy 0. The left shift does not affect the sign bit.

Mathematical significance: On the premise that the number does not overflow, for positive and negative numbers, left one is the equivalent of multiplying by 2 of the 1, left to shift n is the equivalent of multiplying by 2 of the n-th square.

6. Move Right

6.1 Signed right Shift >>: that is, in binary form, all values are shifted to the right but the symbol bits are reserved.

Mathematical significance: in the case of numbers without overflow, for positive and negative numbers, the right shift is equivalent to dividing by 2 of the 1, the right shift n is the equivalent of dividing by 2 of the n-th square.

6.2 Unsigned Right Shift >>>: that is, in binary form, all values, including sign bits, are moved to the right.

For positive numbers >> and >>> results are the same;

For negative numbers, because negative numbers are expressed in the complement of their absolute values, this results in a very large unsigned right shift.

Third, Boolean operators

1, logical not !
Logic is not used! Indicates that a value of any type that can be applied with ECMAScript, the logical non-operation returns a Boolean value (True/false). The operator first converts its operand to a Boolean value and then deserializes it.

!! Equivalent to the Boolean () function.

A set of rules for a Boolean () transformation function. Http://www.cnblogs.com/yxField/p/4211704.html

Data type The value converted to true Value converted 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 object Null
Undefined No Undefined

2. Logic and &&

The logic has two operands.

Logic and operations can be applied to operands of any type, not just Boolean values. In the case where an operand is not a Boolean value, the logic and operation do not necessarily return a Boolean value; At this point, it follows the rules:
1. If the first operand is an object, a second operand is returned;
2. If the second operand is an object, the object is returned only if the value of the first operand evaluates to true;
3. If the two operators are objects, the second operand is returned, followed by the first rule.
4. Returns null if one of the operands is null;
5. If one of the operands is Nan, a nan is returned;
6. If one of the operands is undefined, the undefined is returned.

Logic and operation are short-circuiting operations, that is, if the first operand can determine the result, then the second operand is no longer evaluated. (Can be understood as an internal two return operation). Therefore, when the 4, 5, 6 rules conflict, the principle of short-circuit operation is followed.

 1  var  nul = null   2  var  na =  NaN;  3   Test  4  test = Na&&nul;  5  document.write (test); // nan  
1 var NULL  2var na =3var4 test = nul&&   5//

3. Logic or | |

Logical OR has two operands.

Logical or similar to logic, operations can be applied to operands of any type, not just Boolean values. In the case where an operand is not a Boolean value, the logic or operation does not necessarily return a Boolean value; At this point, it follows the 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 all two operators are objects, the first operand is returned, followed by the first rule.
4. Returns null if all two operands are null;
5. If the two operands are Nan, a nan is returned;
6. If the two operands are undefined, the undefined is returned.

A logic or operation is a short-circuit operation, that is, if the first operand results in true, then no second operand is evaluated.

We can use the logic or this feature to avoid assigning null or undefined values to variables.

Example: var myobject=firstobject| | Secondobject

If firstobject is not NULL, Firstobject is assigned to MyObject, otherwise the value of Secondobject is assigned to MyObject.

Learn JavaScript from the beginning (iv)--operator

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.