Basic concepts of JavaScript (iv) --- operators and javascript Operators

Source: Internet
Author: User
Tags bitwise operators

Basic concepts of JavaScript (iv) --- operators and javascript Operators
Operator 1. unary operator

The operator that can operate only one value is called The unary operator. The unary operator includes the increment (decrement) operator and the unary plus (decrement) operator.

The increment/decrease operator is divided into the increment/decrease operator and the increment/decrease operator. The increment/decrease operator is used in the same way as the increment/decrease operator in java, the difference is that the incrementing and decreasing Operators in javascript can operate non-numeric values. See the following example:

 

A. When you perform the increment/decrease operation on a string that contains valid numeric characters, the string is converted to a number before performing the increment/decrease operation. The string is finally converted to the number type.

B. For strings that cannot be converted into numbers, NaN is returned and the original string becomes NaN.

C. For Boolean values, convert true to 1, and convert false to 0, and then perform the incremental and progressive decrease operation.

D. object type. First, execute the valueOf () method of the object to incrementally and progressively decrease the retrieved value. If NaN is returned, then execute the toString () method, the above method is also used to increase and decrease progressively.

The value of a variable changes before the statement is evaluated. The value of a variable changes after the statement is evaluated.

For The unary plus or minus operators, The unary plus operator is represented by (+), and The unary minus operator is represented. The unary addition or subtraction operator for a Number is the same as the addition and subtraction operator in mathematics, but when the unary addition and subtraction operator is used for a non-numeric value, it is equivalent to using the Number () function for the current value. The specific operations are as follows:

 

 

2. bitwise operators

The bitwise operator is actually a bit of a value in the Operation memory. All values in ECMAScript are stored in the IEEE-754 64-bit format, but the bit operator does not directly operate the 64-bit value, but first converts the 64-bit value to 32-bit, and then performs the operation, switch the operation result back to 64-bit.

For an integer with a negative number, the first 31 digits of the 32-bit value are stored as values, and the 32-bit value isSymbol bit, 1 indicates a negative number, and 0 indicates a positive number. Where positive numbers are stored in pure binary format, each of the 31 bits is an exponential power of 2, and the index of 2 starts from 0 and is always backward.Unused bits are filled with 0.Negative numbers are also stored in binary format, but the format is binary complement.(Baidu for the calculation method ). In ECMAScript, the binary representation of a negative number is a negative number before the binary value of the negative number. For example, if the binary code of 18 is 10010, the binary code of-18 is-10010.

When the bitwise operator is applied to a value in ECMAScript, the 64-bit value is first converted to a 32-bit value, and then the 32-bit result is converted back to a 64-bit value. When NaN and Infinit use bitwise operators, they are processed as 0. ForNon-numeric value, used automaticallyNumber ()After the function is converted to a value, bitwise operations are performed to obtainNumeric result.

2.1 Non-bitwise

Use wavy lines (~) The bitwise non-operator returns the inverse code of the value, as shown in the following example:

 

We can see that after a non-bitwise operation is performed on 25, the result returns-26, and the binary code of-26 is a binary code plus a negative number before the binary code of 26, which should be a binary complement code, however, ECMAScript displays the negative value of the binary code of an integer. The preceding example shows thatThe bitwise non-operator is equivalent to the negative value of the current value minus one..

2. bitwise AND | bitwise OR

Bitwise AND operator(&)This operator operates on the two operands and compares the values at the corresponding positions of the two values according to the rules in the following table. A value is returned. The comparison rules are as follows (The values at the same position of the two values are1Result 1Other results are 0.):

The bit of the first value.

The second digit.

Comparison results

1

1

1

1

0

0

0

1

0

0

0

0

Example:

 

 

Bitwise OR operator(|)The comparison rules are as follows (If one of the two values at the same position is1The result is 1.):

The bit of the first value.

The second digit.

Comparison results

1

1

1

1

0

1

0

1

1

0

0

0

Example:

 

Bitwise OR (^) is used to operate two operands. Comparison rules (The two values are at the same position and the result is0The result is 1.):

The bit of the first value.

The second digit.

Comparison results

1

1

0

1

0

1

0

1

1

0

0

0

Example:

 

 

2. 3. Move left

<) Indicates the Left shift. As follows:

 

As shown in the preceding example,The Left shift operator moves the position of a value to the specified number of digits on the left.That is, move the value to the left five places, and then the right side of the original value is empty for five positions. Use 0 to fill the space. Note:The left-shift operator does not affect the symbol bit of the operand.That is to say, if-11 is used to move 5 digits to the left, the result is-352.

 

. Shifts the signed right | shifts the unsigned right

The signed right shift is represented by (>). Let's look at the example first:

 

A signed right shift moves the value to the right of the specified number of digits, but retains the symbol bit. That is to say, a signed right shift will not change the value, the vacancy caused by right shift is filled with the value of the symbol bit to get a complete value.

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

1

0

0

0

0

0

0

0

The value 128 is converted to a 32-bit binary code. As shown above, the position indicated by the arrow isSymbol bit

 

 

 

 

 

The result is 4 after 5 digits are shifted to the right. The binary code is as follows:

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

1

0

0

 

The unsigned right shift is represented by (>>>). This operator moves all 32 bits of the value to the right,For positive numbers, the result of the unsigned right shift and the signed right shift is the same.. However, for negative numbers, the result will change because of two main reasons: first, the unsigned right shift is to fill the blank space with 0, and the signed right shift is to fill the blank space with the value of the symbol bit; second, the unsigned right shift regards the negative binary code as a positive binary code, and because the negative binary code exists in the binary code complement form of its absolute value, therefore, the result after the unsigned right shift is very large.

Example:

 

As shown above, the-64 binary code is a 64-bit binary code, that is, 1111111111111111111111111000000,

The unsigned right shift operation regards this value as a positive binary code, and then shifts 5 digits to form a 00000111111111111111111111111110 value, which is converted to a decimal number of 134217726.

 

3. boolean operator 3. 1. Non-logical

Logical non-operator (!) This operator can be used for any value in ECMAScript to return a Boolean value. When this operator is used for the value, the current value is first converted to a Boolean value, and then reverse it. The specific conversion rules are as follows:

I. If the operand is an object, false is returned;

Ii. If the operand is a Null String, true is returned;

Iii. If the operand is a non-null string, false is returned;

Iv. If the operator is 0, true is returned;

Vi. If the operand is not 0 (including Infinity), false is returned;

Vii. If the operand is null, true is returned;

Viii. If the operand is NaN, true is returned;

Ix. If the operand is undefined, true is returned;

 

3. 2. Logic and

The logic and operator are represented by the (&) symbol. Like in java, true is returned only when both operands are true. If one side is false, false is returned. However, in javascript, the Data Types of the two operands are not necessarily Boolean or other data types. The judgment rules for the returned results are as follows:

I. If the first operand is an object, the second operand is returned;

Ii. If the second operand is an object, the object will be returned only when the first operand returns true;

Iii. If both operands are objects, the second operand is returned;

Iv. If an operand is null, null is returned;

V. If an operand is NaN, NaN is returned;

Vi. If an operand is undefined, undefined is returned;

Note: The logic and operation are short-circuit operations. As long as the first operand can determine the result, the second operand will not be evaluated. The two operands cannot contain undeclared variables. Otherwise, an error occurs.

 

3. Logic or

The logic or operator is represented by (|). If either of the two operands is true, the result is true. Similarly, if one of the two operands is not a Boolean value, the logic may not necessarily return a Boolean value. The specific rules are as follows:

I. If the first operand is an object, the object is returned;

Ii. If the evaluate result of the first operand is false, the second operand is returned;

Iii. If both operands are objects, the first operand is returned;

Iv. If both operands are null, null is returned;

V. If both operands are NaN, NaN is returned;

Vi. If both operands are undefined, undefined is returned;

 

4. Multiplication Operators

ECMAScript specifies three multiplication operators, namely multiplication, division, and modulo. The three operators are the same as the operations in java. The difference is that when the operators are not numerical values, automatic type conversion is performed.

4. 1. Multiplication

The multiplication operator is represented by (*). It is used to calculate the product of two values. When the operand is special, the processing rules are as follows:

I. If both operands are numerical values, they can be calculated by mathematical multiplication. However, if the calculation result exceeds the value range specified by ECMAScript, Infinity or-Infinity is returned;

Ii. If one of the operands is NaN, NaN is returned;

Iii. If Infinity is multiplied by 0, the result is NaN;

Iv. If Infinity is multiplied by a non-zero value, the result is Infinity or-Infinity. the symbol depends on a non-zero value;

V. If Infinity is used to multiply Infinity, the result is still Infinity;

Vi. If one of the operands is not a value, call the Number () function to convert it to a value, and then perform the preceding operations.

 

4.2. Division

The Division operator is expressed by (/), indicating that the second operand is used except the calculation of the first operand. In special cases, the processing rules are as follows:

I. If both operands are numerical values, perform mathematical calculations. If the operator exceeds the value range specified by ECMAScript, Infinity or-Infinity is returned.

Ii. If one of the operands is NaN, NaN is returned;

Iii. If Infinity is divided by Infinity, the result is NaN;

Iv. If it is 0 divided by 0, the result is NaN;

V. If a non-zero finite number is divided by 0, Infinity or-Infinity is returned. The symbol is dependent on the devisor;

Vi. If Infinity is divided by any non-zero finite number, the returned result is Infinity or-Infinity.

Vii. If one of the operands is not a numerical value, call the Number () function to convert it to a numerical value, and then perform the preceding operation.

4. modulo

The modulo operator (%) represents the remainder of the first operand divided by the second operand. The processing rules are as follows:

I. If both operands are numerical values, perform a mathematical operation;

Ii. If the divisor is infinitely large and the divisor is finite, NaN is returned;

Iii. If the divisor is infinitely large and the divisor is 0, NaN is returned;

Iv. If Infinity is divided by Infinity, the result is NaN;

V. If the divisor is a finite value and the divisor is an infinitely large value, the devisor is returned;

Vi. If the divisor is 0, the result is 0;

Vii. If one of the operands is not a numerical value, call the Number () function to convert it to a numerical value, and then perform the preceding operation.

 

5. Addition Operators 5. 1.

The addition operator is represented by (+). If both operands are numerical values, perform the regular addition operation and calculate the result according to the following rules:

I. If one number is NaN, the result is NaN;

Ii. If it is Infinity plus Infinity, the result is Infinity;

Iii. If it is-Infinity plus-Infinity, the result is-Infinity;

Iv. If it is Infinity plus-Infinity, the result is NaN;

V. If it is + 0 plus-0, the result is + 0;

Vi. If it is + 0 plus + 0, the result is + 0;

Vii. If it is-0 plus-0, the result is-0;

 

If one operator is a string, the following rules must be used for a long time:

If an operand is an object, a value, or a Boolean value, call their toString () method to obtain the corresponding string, and then apply the preceding string rules. If it is undefined or null, call the String () function to obtain the corresponding strings "undefined" and "null ";

 

 

5. Subtraction

The subtraction operator is represented by (-). The specific operation rules are as follows:

I. If both operators are numerical values, you can perform mathematical operations;

Ii. If an operand is NaN, the result is NaN;

Iii. If Infinity is used to reduce Infinity, the result is NaN;

Iv. If it is-Infinity minus-Infinity, the result is NaN;

V. If it is Infinity Minus Infinity, the result is Infinity;

Vi. If it is-Infinity Minus Infinity, the result is-Infinity;

Vii. If it is + 0 minus + 0, the result is + 0;

Viii. If it is + 0 minus-0, the result is-0;

Ix. If it is-0 minus-0, the result is + 0;

X. If an operand is a string, Boolean value, null, or undefined, call the Number () function to convert it to a value and perform subtraction based on the preceding rules;

Xi. if one of the operands is an object, call the valueOf () method of the object to obtain the value of the object. If the obtained value is NaN, the result of the subtraction is NaN, if the object does not have the valueOf () method, call the toString () method and convert the obtained string to a number.

 

6. Relational operators

Use <, >,<=,> = in ECMAScript to compare the values of the two Relational operators. The specific comparison rules are as follows:

I. If both operands are numerical values, perform mathematical operations;

Ii. If both operands are strings, compare the character encoding values corresponding to the two strings;

Iii. If one operand is a numerical value, convert the other operand to a numerical value and perform numerical comparison;

Iv. if an operand is an object, first call the valueOf () method of the object and use the obtained value to compare it according to the preceding rules. If the object does not have the valueOf () method, then the toString () method of the object is called, and the obtained value is compared according to the previous rule.

V. If an operand is a Boolean value, replace it with a numerical value before comparison.

The following are two examples:

 

In this example, a is less than B but greater than B, becauseComparing two strings is to compare the character encoding values of each character in the corresponding position of the two strings., The character encoding values of uppercase letters are all less than the character encoding values of lowercase letters, so a <B returns false.

Example 2:

 

In this example, we first compare the string "23" with the string "3". The comparison result is "23" less than "3", for the same reason as in the previous example,The two strings compare character encoding. The "23" character encoding is 50, and the "3" character encoding is 51.So the returned result is true. For the comparison between string "23" and number 3, because an operand is a number, the string is converted to a number for comparison, so false is returned.

 

7. Equal Operators

Equality operators are divided into two types. The first type is equal and not equal. When conversion is required, conversion is performed before comparison. The second type is full and incomplete. At this time, conversion is not performed, directly compare.

7. 1. Equal and unequal

Equal operators are represented by (=), and unequal operators are represented (! =) Indicates that both operators perform a forced transformation on the operands, and then compare the equality of the two.

During forced transformation, equal and unequal operators follow the following rules:

I. If one of the operands is a Boolean value, convert it to a number first, false is 0, true is 1;

Ii. If one operand is a string and the other operand is a number, convert the string to a number before comparison;

Iii. If one operator is an object and the other is not, call the valueOf () method of the object and compare it with the obtained basic type values according to the previous rules;

Iv. null and undefined are equal;

V. Before comparison, you cannot convert null or undefined to another value;

Vi. If one of the operands is NaN, the equal operator returns false. The unequal operator returns true. If both operands are NaN, the equal operator returns false;

Vii. If both operands are objects, compare whether the two operators direct to the same object. If yes, the equal operator returns true; otherwise, the return value is false;

. Full and incomplete

The only difference between equality and inequality is that operations are not converted before comparison. All operators are represented by (=, returns true if the two operands are equal without conversion. They are not completely used (! =.

Note: null = undefined returns true because the two are similar values, but null = undefined returns false because the two are not a type. We recommend that you use full and incomplete operators.

 

8. Conditional operators, value assignment operators, and comma operators 8. 1. Conditional Operators

The condition operator is the ternary comparison to which we belong. The expression is: boolean_condition? True_value: false_value. If the condition is true, true_value is returned. If the condition is false, false_value is returned.

 

. Value assignment operator

The value assignment operator assigns the value on the right of the operator to the variable on the left. The simplest value assignment operator is =. Other operators include * =,/=, % =, + =, -=, <= (left shift assignment), >=( shifted to the right with a symbol), >>=( shifted to the right without symbol)

 

8.3 comma Operator

You can use the comma operator to execute multiple operations in a statement, as shown below:

Var num1 = 1, num2 = 2, num3 = 3;

 

 

The operator part has finally ended. So tired.

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.