JavaScript Advanced Programming (3) Basic concepts

Source: Internet
Author: User
Tags arithmetic operators bitwise bitwise operators

Operator

ECMA-262 describes a set of operators that manipulate data values, including arithmetic operators, bitwise operators, relational operators, and equality operators. They can adapt to many values, such as strings, numeric values, Boolean values, and even objects. When an object is applied, the corresponding operator invokes the valueof () and ToString () methods of the object. Gets the value that can be manipulated.

Unary operator: Only one value can be manipulated.

1. Increment and decrement operators.

Pre-++i: + + is positioned before the variable to be manipulated with a variable of 1 and then assigned to the left variable.

Post-value (i++): + + is positioned after the variable to be manipulated, first assigned to the left variable, and then +1.

Diminishing the same;

var  NUM1 = 2; var  num2 =; var  num3 =--num1 + num2;           // equal to var  NUM4 =  num1 + num2;            // equal to var num5  = num1--+num2;            //  var num6  = num1 + num2;             // equal to

They apply not only to integers, but also to strings, Boolean values, floating-point values, and objects. There are different rules for applying different values.

    1. When a string contains a valid numeric character, it is first converted to a numeric value, and then the operation with the addition minus 1 is performed. The string variable becomes a numeric variable.
    2. When a string does not contain a valid numeric character, the value of the variable is set to Nan. The string variable becomes a numeric variable.
    3. A Boolean value of false, first converting it to 0 and then performing a plus minus 1 operation. A Boolean variable becomes a numeric variable.
    4. Boolean true to convert it to 1 before performing a plus minus 1 operation. The Boolean value becomes a numeric variable.
    5. For floating-point values, perform a plus minus 1 operation.
    6. Object, the ValueOf () method of the object is called first to obtain a value that can be manipulated, and then applies the aforementioned rule to the value. If the result is Nan, the aforementioned rule is applied after the ToString () method is called. The object variable becomes a numeric variable.
varS1 = "2";varS2 = "Z";varb =false;varF = 1.1;varn \{valueOf:function(){  return-1;}}; S1++;//The value becomes the number 3s2++;//value becomes Nanb++;//The value becomes the number 1f--;//value becomes 0.10000000000000009 (floating-point rounding error)o--;//value becomes numeric-2

2. Unary Plus and minus operators

denoted by a plus sign (+), placed before the value, has no effect on the value. This value is converted before a non-value.

is represented by a minus sign (-), placed before the value, and becomes a negative number. The value is converted to a negative number before it is non-numeric.

Bitwise operators

The bitwise non (not) wavy line (~) indicates that the result is the inverse of the value returned.

As indicated by the (and) and ampersand characters (&), only the corresponding bit of two numeric value returns 1, and any one is 0, and the result is 0.

A bitwise OR (or) vertical bar symbol (|) indicates that a bit is 1 and returns 1, and only two bits are 0 in the case of 0.

The bitwise XOR (XOR) caret (^) indicates that only one 1 o'clock in the corresponding bit of two values returns 1, or 0 if the corresponding two bits are 1 or both are 0.

Moving left two less than (>>) indicates that all bits of a numeric value are shifted to the left by the specified number of digits. Symbol bits that do not affect operands

A signed right shift of two greater than sign (<<) means that the value is shifted to the right, with the sign bit filling all the slots.

Unsigned right shift three greater than sign (<<<), the binary number is shifted to the right, directly to the right, for positive numbers, the result is the same as >>, for negative numbers, the binary complement of negative numbers as positive binary code processing

Boolean operator

Logical non (NOT): Exclamation mark (! ) indicates that its operand is converted to a Boolean value, and then the negation is reversed.

You can convert a value to a true Boolean value by using two logical non-operators at the same time.

Logic and (and): two ampersand (&&) is indicated.

Returns True when the corresponding Boolean value for the two operands is true, and a false returns false.

Short-circuit operation: If the first operand can determine the result, then the second operand is no longer evaluated.

Logical OR (and): Two vertical bar symbols (| | Said

Returns False when the corresponding Boolean value of the two operands is false, and a true returns TRUE.

Short-circuit operation: The first operation results in true, so the second operand is not evaluated.

Calculation operators

The multiplication operator is represented by an asterisk (*), which calculates the product of two values.

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

The modulo (remainder) operator is represented by a percent semicolon (%). Performs a general division calculation, returning the remainder that is removed

Additive operator (+)

If it is a numeric value, perform a regular addition calculation.

If there is a string, it will be stitched together. By adding parentheses (), you can perform arithmetic calculations on numeric values and then stitch the results together with strings.

Subtraction Operator (-)

If it is a numeric value, perform a general subtraction calculation,

If it is the other, it will be converted, in the calculation

1. In addition to plus (+), if the operand is not of type number, it will be automatically converted to the # type and then evaluated.

2, for addition and subtraction (+-), except as an arithmetic operator. It can also be used as a unary operator (see above). Of course, because of the overloading of the plus sign (+) in the string operation, it can also be used to connect any numeric value (the string), which is also the 1th why in addition (+), when it contains a non-number type value, it will convert all operands to a string connection.

3, unlike the General C language, in Es, except (/) and modulo (%) does not distinguish between integers and floating-point numbers, such as 5/2 = 2.5 instead of 2,5.3% 3 = 2.3 instead of 2.

4, arbitrary operation, as long as the operand contains Nan, the result is Nan. But it's not as if the result is Nan there must be an operand of Nan, for example 0/0 also returns Nan.

5, for the operation with infinite infinity, the provisions are more, here is not listed, can refer to the original book, or self-test.

Relational operators

A Boolean value is returned for comparisons of two values (<), greater than (>), less than or equal (<=), and greater than or equal (>=).

1. If the two operands are numeric, perform a numeric comparison.

2. If the two operands are strings, the corresponding character encoding value is compared.

3, when the operator is an object, call valueof () (if not, call ToString ()), and then compare the result to the rule above.

4, any number and Nan compare return false.

Equality operators

equals sign (= =) does not equal (! =) Convert the operands first, and then compare their equality.

Congruent (= = =) Not congruent (!) = =) does not convert operands, directly compared.

A===b must have a==b, and a!=b must have a!==b.

Conditional operator

Variable = boolean_expression? True_value:false_value;

The expression is based on conditionally assigning a value to a variable according to the results of the boolean_expression calculation. If boolean_expression is true, the true_value is assigned to the variable, and if it is false, the False_value is assigned to the variable.

The value of the right of the assignment operator (=) is assigned to the variable on the left.

There are compound assignment operators for each of the major arithmetic operations and several other operations:

    • Multiplication/Assignment (*=)
    • Division/Assignment (/=)
    • Modulo/Assignment (%=)
    • Addition/Assignment (+ =)
    • Subtraction/Assignment (-=)
    • Shift Left/Assignment (<<=)
    • Signed Right Shift/Assignment (>>=)
    • Unsigned Right Shift/assignment (>>>=)

Comma operator

You can use the comma operator to perform multiple operations in a single statement.

var 1 2 3;

Many are used to declare multiple variables.

JavaScript Advanced Programming (3) Basic concepts

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.