JavaScript operators detailed

Source: Internet
Author: User
Tags arithmetic arithmetic operators array arrays expression functions variables string

Core Tip:JavaScript has the following kinds of operators: arithmetic operators, equivalent operators and all-in-one operators, comparison operators;

1. JavaScript has the following kinds of operators: arithmetic operators, equivalent operators and all the same operators; comparison operators;

2. Purpose Category: string operator; logical operator; bitwise operator; assignment operator;

3. Special operators: Conditional operators; typeof operators; creating object operators new;delete operators; void operation symbols; comma operators;

Arithmetic operators: +,-, *,/,%,-(unary inverse), + +,-

The equivalent operator and the full operator: = =, = = =,!==,!===

Comparison operators:<, >, <=, >=

String Operators::<, >, <=, >=, =, +

Logical operators:&&,,!,

Assignment operators: =, + =, *=,-=,/=

Rules:

One, be sure to pay attention to the data type passed to the operator and the data type returned! Different operators expect its operand expression to compute the result that matches a data type.

For example: A string cannot be multiplied, "a" * "B" is illegal, but, where possible, JavaScript converts an expression to the correct type, so the expression "3" * "5" is legal, and JavaScript converts a string into a numeric operation. The result returns the number 15 instead of the string "15".

Second, + according to the different number of operations, with different performance:

String + String = string (connected); A "+" b "=" AB "" 5 "+" 6 "=" 11 "

string + Number = (string converted to numeric) string (connected); A "+ 5 =" A5 "5 is converted to a string" 1 "+ 0 =" 10 "

Number + number = number (ADD) 5 + 5 = 10.

Third, the combination of attention operators, some operators from left to right binding, some from right to left binding.

For example: W = a + B + C is equivalent to W = (A + b) + C;

W =---b is equivalent to W =-(-(b)); W = a = b = C equals w= (a = (b = c))

The binding of a unary operator, an assignment operator, and a ternary operator is from right to left;

Iv. arithmetic operator rules:

"+": Can be "addition" and "connection" operation; if one of the 2 operators is a string, JavaScript converts the other to a string and then joins the 2 operands.

"+": If the arithmetic is an object, JavaScript will convert the object into numbers to do addition or string concatenation operation;

"-" "*"/"If one of the 2 counts is a non-numeric, convert it to a number to perform a mathematical operation.

"/" in JavaScript, because all numbers are floating-point numbers, the result of the division is a floating-point number, 5/2 = 2.5, the result of the divisor 0 is positive and negative, and 0/0 is Nan;

% modulo operator: computes the modulus of the first operand for the second operand, that is, the remainder is returned when the first operand is counted by the second operation. If the OP count is Non-numeric, it is converted to a number.

"-" unary negation: to reverse the arithmetic, if the operand is not a number, then converted to a number.

"+ +" "--" increment operator/decrement operator symbol: OP arithmetic must be a variable, an element of a tree group, or an attribute of an object. If the operand is not a number, it is converted to a number.

Note: if "+ +" is before the count, the operands are incremented and the number of operands is computed.

If the "--" is on the count, first calculate the value before the OP count has not grown, and then increment the count for the op.

For example: i = 1; Assign value to I value 1

j = ++i; The first increment to 2 and then the value assigned to J,i by 2 is the value of 2,j and 2.

i = 1; Assign value to I value 1

j = i++; I assign a value to J, and then the value that increments the I value to 2,i is 2,j is 1.

The decrement operator "--" is the same as "+ +".

The equivalent operator and the full same operator:

(i) "= =" "!==" equals operator and non-equivalent operator:

Compares 2 operands and returns a Boolean value.

Comparison values, strings, and Boolean values are measured. They are equal when and only if the two variables hold equal values.

When comparing objects, arrays, functions, using references, only 2 variables refer to the same object, they are equal

。 Two different arrays are completely different, even if they have exactly the same elements. For variables that hold references to objects, arrays, functions,

Say they are equal only when they refer to the same object, array, function.

Pay attention! Principles to be followed:

When the type of two operands is not the same: convert them to the same type,

1 A number is compared with a string, after the string is converted to a number.

2 true converts to 1, false to 0, for comparison.

3 an object, array, function, and a number or string, objects, arrays, functions are converted to values of the original type, and then compared. (Use valueof First, if not, use ToString)

4 other types of combinations are not stars.

You want to have the same type of two operands, or after converting to the same type:

1) 2 strings: The characters at the same position are equal and 2 strings are the same.

2) 2 digits: 2 numbers are the same, the same. If one is Nan, or two are Nan, it is not the same.

3) 2 are true, or 2 are false, they want to be the same.

4) 2 refer to the same object, function, array, they are equal, if the reference is not the same object, function, array, it is not the same, even if the 2 objects, functions, arrays can be converted to the original value of exactly equal.

5) 2 null, or 2 are undefined, then they are equal.

"!=" non-equivalence operator: Contrary to the result of the equivalent operator detection.

(ii) "= = =" "!===" all of the same operator and the non-identical operation symbol:

The same operator follows the comparison rule of the equivalent operator, but it does not type the operand, returns False when the type of the two operands is not the same, and only follows the comparison rule of the equivalent operator when the type of the two operands is the same.

The "!==" operator is not the same as the result of a comparison of all the operators. Returns true if the type or value of two operands is different.

1 2 Next page > full text reading tips: Try "←→" button, turn the page more convenient Oh!

[1] [2] Next page



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.