JS Basics Review: The syntax of ECMAScript (iii)

Source: Internet
Author: User
Tags bitwise operators

ECMA-262 describes a set of operators that manipulate data values, including arithmetic operators, bitwise operators, relational operators, and equality operators.

The difference between the ECMAScript operators is that they can be applied to many values, such as strings, numeric values, Boolean values, and even objects.

When these operators are applied to an object, the corresponding operator typically invokes the valueof () and/or toString () methods of the object in order to obtain a value that can be manipulated.

An operator that can manipulate only one value is called a unary operator.

The increment and decrement operators draw directly from C, each with a pre-and post-set version of two versions: a++, ++a, a--、--a

These four operators apply not only to integers, but also to strings, Boolean values, floating-point values, and objects.

When applied to different values, the increment and decrement operators follow the following rules:

When applied to a string containing a valid numeric character, it is first converted to a numeric value, and the string variable becomes a numeric variable when the operation is performed plus minus 1;

When applied to a string that does not contain a valid numeric character, the value of the variable is set to Nan, and the string variable becomes a numeric variable;

When applied to the Boolean value false, it is converted to 0 and then the operation plus minus 1, the Boolean variable becomes a numerical variable;

When applied to a Boolean value of true, it is converted to 1 and then the operation plus minus 1, the Boolean variable becomes a numerical variable;

When applied to floating-point values, perform a plus minus 1 operation;

When applied to an object, the object's valueof () method is called to obtain a value that can be manipulated, then the value is manipulated according to the above rule, and if the result is Nan, then the ToString () method is called before the rule is applied, and the object variable becomes a numeric variable.

The unary plus operator is represented by a plus sign (+).

The unary plus operator is preceded by a numeric value and has no effect on the value; When you apply a unary operator to a non-numeric value, the operator performs a conversion to the value of the number () function.

The unary minus operator is mainly used to represent negative numbers, denoted by a minus sign (-).

When a unary minus operator is applied to a value, the value becomes negative, and when applied to a non-numeric value, the unary minus operator follows the same rule as the unary plus operator and finally converts the resulting value to a negative number.

The unary plus and minus operators are primarily used for basic arithmetic operations, and can be used to convert data types as shown earlier.

Bitwise operators are used to manipulate values at the most basic level, that is, by the bits in memory that represent the values.

All the values in ECMAScript are stored in the IEEE-754 64-bit format, but the bitwise operator converts the value of 64 to a 32-bit integer, then performs the operation and then converts the result to 64 bits, but the process is transparent to the developer. So the whole process is like an integer that only has 32 bits.

For signed integers, the first 31 bits in the 32-bit are used to represent the value of an integer, and the 32nd bit is used to represent the symbol for the numeric value: 0 for positive numbers and 1 for negative numbers.

The bit that represents the symbol is called the sign bit, and the value of the symbol bit determines the format of the other bit values.

Where positive numbers are stored in a pure binary format, each of the 31 bits represents a power of 2, the first bit (called bit 0) represents the 0 power of 2, the second represents the 2 power of 1, and so on, the unused bits are filled with 0.

In addition, negative numbers are stored in the form of twos complement: First, the binary code of the absolute value is obtained, then the inverse code of the binary code (replacing 0 with 1, replacing 1 with 0), the binary inverse code plus 1 is obtained.

ECMAScript will try to hide all this information from us, and when we output a negative number in binary form, we only see a minus sign in front of the binary code with the absolute value of the negative number.

When you are working with signed integers, you cannot access bit 31 (that is, the 32nd bit), and by default all integers in ECMAScript are signed integers.

Of course there are unsigned integers, and for unsigned integers the 32nd bit no longer represents symbols because unsigned integers can only be positive numbers, unsigned integers because they have one extra bit to represent a larger number.

When you apply a bit operation to a special Nan and infinity value, both values are treated as a limit.

If you apply a bitwise operator to a non-numeric value, the number () function is used first to convert it to a number (auto-complete) and then the bitwise operator, and the result is a numeric value.

A bitwise non-operator is represented by a wavy line (~), and the result of performing a bitwise non is the inverse of the value returned.

Although the essence of bitwise non-operation is that the negative value of the operand is minus 1, it is faster than the direct operation because it is the underlying operation.

The bitwise AND operator is represented by a and number character (&), which has two operands.

In essence, bitwise AND operation is to align each bit of the two numeric values, and then do and manipulate for each bit.

Bitwise AND Operation returns 1 only if the corresponding bit of the two value is 1, and any one is 0 the result is 0.

A bitwise OR operator is represented by a vertical bar symbol (|), which has two operands.

In essence, a bitwise OR operation is the alignment of each bit of the two numeric values, and then it is done or manipulated for each bit.

A bitwise OR operation returns 0 only if the corresponding bit of the two value is 0, and any one is 1 and the result is 1.

The bitwise XOR operator consists of a caret (^), which has two operands.

In essence, bitwise XOR or manipulation is the alignment of each bit of the two numeric values, and then the XOR or manipulation for each bit.

Bitwise XOR OR operation only one of the corresponding bits of two values is 1 o'clock returns 1, and if two values correspond to both 0 or 1 o'clock, 0 is returned.

The left shift operator is represented by two less than (<<), which moves all bits of all numeric values to the left by the specified number of digits: oldvalue<<5 (five bits to the left)

When shifted to the left, the extra space on the right side of the original value is padded with 0 so that the resulting result is a full 32-bit binary number.

The left shift does not affect the sign bit of the operand, in other words, if you move 2 to the left 5 bits, the resulting result will be-64.

The signed right shift operator is represented by the two greater than sign (>>), which moves the value to the right, but retains the sign bit, and the empty space generated during the right shift is populated with the sign bit.

The unsigned Right shift operator is represented by the three greater than sign (>>>), which moves all 32 bits of the value to the right.

Since the unsigned right-shift operator will treat all binary numbers as positive numbers, and fill the moved slots with 0.

So for positive numbers, the result of the unsigned right shift is the same as the signed right shift, but it makes a big difference for negative numbers.

There are a total of three Boolean operators: Non (not), with (and), or (or).

The logical non-operator consists of an exclamation mark (! ) represents any value that can be applied to the ECMAScript.

A logical non-operator converts its operand, regardless of its data type, first to a Boolean value, and then returns a Boolean value after it is reversed:

Returns False if the operand is an object;

Returns true if the operand is an empty string;

Returns False if the operand is a non-empty string;

Returns true if the operand is a numeric value of 0;

Returns False if the operand is any non-0 value (including infinity);

Returns true if the operand is null;

Returns true if the operand is Nan;

Returns true if the operand is undefined.

If two logical non-operators are used at the same time, the behavior of the Boolean () transformation function is simulated, and the operand is converted to its corresponding Boolean value.

The logic and operator are represented by the two and number (&&), which have two operands and return true only if the value of the two operands is true, otherwise false.

Logic and operations can be applied to any type of operand, not just a Boolean value, and the operation does not necessarily return a Boolean value if the operand is not entirely a Boolean value:

Returns the second operand if the first operand is an object;

If the second operand is an object, the object is returned only if the value of the first operand evaluates to true;

If the two operands are objects, the second operand is returned;

Returns null if one of the operands is null;

If one of the operands is Nan, a nan is returned;

If one of the operands is undefined, the undefined is returned.

Logic and operations are short-circuiting operations, and if the first operand determines the result, the second operand is no longer evaluated.

Using undefined values in logic and operations results in an error, but if the value of the first operand is false and the second operand is undefined, no error is given because the second operand is not evaluated.

A logical OR operator consists of two vertical bars (| | ) indicates that there are two operands that return false only if the value of the two operands is false, otherwise true.

A logical OR operation can be applied to any type of operand, not just a Boolean value, and the operation does not necessarily return a Boolean value if the operand is not entirely a Boolean value:

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

Returns the second operand if the evaluation result of the first operand is false;

If the two operands are objects, the first operand is returned;

Returns NULL if all two operands are null;

If the two operands are Nan, a nan is returned;

If the two operands are undefined, the undefined is returned.

Like logic and operations, a logic or operation is also a short-circuit operation, and if the result of the first operand is true, the second operand is no longer evaluated.

We can use the logic or this behavior to avoid assigning null or undefined values to variables, for example: var myobject=preferredobject| | Backupobject;

In this example, if the value of Preferredobject is not NULL, then it will be assigned to MyObject, and if it is null, the assignment statement that assigns Backupobject to the Myobject,ecmascript program will often use this pattern.

ECMAScript defines three multiplicative operators: multiplication, division, and modulo.

If an operand that participates in multiplicative calculations is not a numeric value, the background will first use the number () transformation function to convert it to a numeric value.

The multiplication operator is represented by an asterisk (*) to calculate the product of two numbers.

In cases where special values are handled, the multiplication operator follows the following special rules:

If the operands are numeric, perform a regular multiplication calculation, and if the product exceeds the representation range of the ECMAScript value, the Infinity or-infinity is returned;

If only one operand is Nan, the result is Nan;

If the infinity is multiplied by 0, the result is Nan;

If the Infinity is multiplied by a value other than 0, the result is Infinity or-infinity;

If there is an operand that is not a numeric value, call number () in the background to convert it to a numeric value and then apply the rule above.

The division operator is represented by a slash sign (/) that performs the calculation of the second operand in addition to the first operand.

The division operator also has special processing rules for special values:

If the operand is a numeric value, perform a general division calculation and return Infinity or-infinity if the quotient exceeds the ECMAScript value range;

If one of the operands is Nan, the result is Nan;

If the infinity is infinity, the result is Nan;

If 0 is removed by 0, then the result is Nan;

If a non-zero finite number is removed by 0, the result is Infinity or-infinity;

If Infinity is removed by any non-0 value, the result is Infinity or-infinity;

If there is an operand that is not a numeric value, call number () in the background to convert it to a numeric value and then apply the rule above.

The modulo (remainder) operator is represented by a percent semicolon (%).

Similar to the other two multiplicative operators, the modulo operator follows the following special rules:

If the operands are numeric, perform a general division calculation and return the remainder;

If the divisor is an infinity value and the divisor is a finite number, the result is Nan;

If the divisor is a finite large number and the divisor is zero, the result is Nan;

If the infinity is infinity, the result is Nan;

If the divisor is a finite number and the divisor is an infinite number, the result is a divisor;

If the divisor is zero, the result is 0;

If there is an operand that is not a numeric value, call number () in the background to convert it to a numeric value and then apply the rule above.

In ECMAScript, the addition and subtraction of two additive operators have a series of special behaviors.

The addition operator (+) performs a regular addition calculation when two operators are numeric, and then returns the result according to the following rules:

If one of the operands is Nan, the result is Nan;

If it is infinity plus infinity, then the result is infinity;

If it is-infinity plus-infinity, then the result is-infinity;

If it is Infinity plus-infinity, the result is Nan;

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

If it is-0 plus-0, then the result is-0;

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

However, if you have an operand that is a string, you should follow these rules:

If the two operands are strings, the second operand is stitched together with the first operand;

If only one operand is a string, the other operand is converted to a string, and then the two strings are stitched together.

Ignoring the data type of the addition operator is one of the most common errors in ECMAScript, and if you want to solve the value first and then join the string, remember to add parentheses.

The subtraction operator (-) also needs to follow some special rules when dealing with various data type conversions:

If all two operators are numeric, perform a regular arithmetic subtraction operation and return the result;

If one of the operands is Nan, the result is Nan;

If it is infinity minus infinity, the result is Nan;

If it is-infinity minus-infinity, the result is Nan;

If it is Infinity minus-infinity, the result is Infinity;

If it is-infinity minus Infinity, the result is-infinity;

If +0 minus +0, the result is +0;

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

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

If an operand is a string, Boolean, NULL, or undefined, the number () function is now called in the background to convert it to a numeric value, and then the subtraction calculation is performed based on the preceding rule;

If an operand is an object, the object's valueof () method is called to get the numeric value that represents the object, and if the object does not have a valueof () method, its ToString () method is called and the resulting string is converted to a numeric value.

The relational operators that are less than (<), greater than (>), less than or equal (<=), greater than or equal (>=) are used to compare two values, and these operators return a Boolean value.

As with other operators in ECMAScript, data conversions are also performed when the operands of a relational operator use a non-numeric value:

If the two operands are numeric, a numeric comparison is performed;

If the two operands are strings, compare the character encoding values of the two strings, which are encoded by bit comparison characters, all uppercase letters are smaller than lowercase letters, and the two numbers are compared by bit when compared.

If one operand is a numeric value, the other operand is converted to a numeric value, and then a numeric comparison is performed;

If an operand is an object, call the ValueOf () method of the object and use the resulting result to perform the comparison with the previous rule, and if the method is not available, call the ToString () method and compare the returned results.

If an operand is a Boolean, it is converted to a numeric value before the comparison is performed.

In common sense, if a value is not less than another value, it must be greater than or equal to that value, but Nan will return false when any comparison is performed with any operand.

Two sets of equality operators are provided in ECMAScript: Equality and inequality-first conversion and comparison, congruent and non-congruent-only compared without conversion.

Equality operation in ECMAScript identifier (= =), unequal operation identifier (! =).

Both operators convert the operands first and then compare their equality with the following rules:

If one of the operands is a Boolean value, it is converted to a numeric value before the equality is compared, and false is converted to 0,true to 1;

If one operand is a string and the other operand is a numeric value, the string is converted to a numeric value before equality is compared;

If one operand is an object and the other operand is not, then the valueof () method of the object is called, and the resulting base type value is compared with the preceding rule;

Null and undefined cannot be converted to any other value until equality is compared;

If the two operands are objects, compare whether they point to the same object.

The subscript lists some special cases and the comparison results:

Null==undefined//true;undefined==0//false;null==0//false

"NaN" ==nan//false;5==nan//false;nan==nan//false;nan!=nan//true

False==0//true;true==1//true;true==2//false; "5" ==5//true

ECMAScript in the same operation identifier (= = =), not congruent operation identifier (! = =).

The congruent comparison returns true if the two operands are equal without conversion, otherwise false is returned.

Note: Null==undefined will return true because they are similar values, and null===undefined will return false because they are different types of values.

Because of the problem of data type conversions for equality and inequality, it is more recommended to use congruent and non-congruent in order to maintain the integrity of data types in your code.

Conditional operator Variable=boolean_expression?true_value:false_value;

The meaning of this operation is based on the result of boolean_expression evaluation, if true, assigns a value to the variable variable true_value, if False, assigns a value false_value to the variable variable.

A simple assignment operator is represented by an equal sign (=), and its function is to assign the value on the right to the variable on the left.

If you add the multiplicative operator, the additive operator, and the bitwise operator before the equal sign, you can complete the compound assignment operation.

The main purpose of designing these operators is to simplify the assignment operation, which does not result in any performance improvement.

You can use the comma operator to perform multiple operations in a single statement, for example: Var num1=1,num2=2,num3=3;

The

Comma operator can also be used to assign values, and the last item in the expression is always returned with a comma operator, for example: Var num= (5,1,4,8,0);//num value is 0

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.