Types of operators in JavaScript and their rules introduction _ Basics

Source: Internet
Author: User
Tags arithmetic arithmetic operators numeric joins logical operators
There are many operators in JavaScript, which are mainly arithmetic operators, equivalent to all the same operators, comparison operators, string operators, logical operators, assignment operators, and so on. These operators have some of their own operation rules, and here is a description of the operators in JavaScript.

   type of javascript operator

1, arithmetic operators: +,-, *,/,%,-(unary counter), + +,-

2, equivalent to all the same operators: = = = = = = =,!==,!===

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

4. String operators:<, >, <=, >=, =, +

5, logical operator:&&, | |,!,

6. Assignment operators: =, + =, *=, =,/=

   Ii. rules for JavaScript operators

1. 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 "+ +".

2, the equivalent operator and the full same operator

(I) the "= =" "!==" equivalent operator and the 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, 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 non-identical operation symbols:

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.

3. Comparison operators

These comparison operators are used to compare different types of values, and the result returns a Boolean value.

"<" ">" "<=" ">="

Note rule: The operands of the comparison can be of any type, but can only be performed on numbers and operands. Operands that are not numbers and strings are converted to numbers or strings.

1 if the two operands are numeric, or can be converted to a number, the size of the number is compared;

2 If the two operands are strings, or both can be converted to strings, they will be compared in alphabetical order;

3 If the string encounters a number, the string is converted to a number for comparison.

4 If the OP arithmetic can neither be converted to a number nor converted to a string, the result returns false.

4, String operators

There are no dedicated string operators, except that some operators behave differently when they encounter string operands.

(I) "+" to connect 2 strings;

1 when the 2 operands are strings, connect them;

2 when one of them is a number, converts the number to a string and joins it;

(II) The comparison operator, ">", compares the order of characters by comparing the order of two strings, the smaller one in front of the teaching, and the upper case before the lowercase letter.

(III) The action method of "+" depends on the order of calculation,

such as: s = 1 + 2 + "var" then: return the result 3var; Because the 1+2 is computed first, then the result 3 is converted to a string and "Var" is connected;

such as: s = "var" + 1 + 2: Returns the result var12; Because the connection between var and 1 is calculated first, the result is var1 with the 2 converted to a string.

5. Logical operators

Used to perform Boolean operations, often used in conjunction with comparison operators, to represent complex comparison operations.

"&&" Logic and Operations, "| |" Logical OR operator, "!" Logical non-operator

(I) "&&" when the two operands are Boolean, the logic and operations are performed on them, that is, if and only if the two Boolean values are true, return the result true, or false.

Note: Actual performance

"&&" will detect the first expression of the Boolean value, if the first op-arithmetic expression returns false, the value of the first operand expression on the left is returned: false; otherwise, the second-right operator expression will continue to be detected, and then the value of the second operand expression is returned;

For example: if (a = b) stop (); With (a = b) && stop (); Equivalent

This method is not supported because the code on the right side of the operator is not guaranteed to be executed.

For example: if ((a < b) && (b++ <10)) Stop (), if the increment on the right side of the operator must be, it may not be executed because of the previous return value, generally against using an expression with other functions on the right side of && (Assignment, function call, increment or decrease);

It is simpler and more secure to consider "&&" as an operator of a Boolean algebra.

(II) "| |" When the two operands are Boolean, the logic or operation is performed on them, that is, when two Boolean values are true, the result returns True, or false.

Note: Actual performance

"| |" The first expression evaluates to a Boolean value, and if the first arithmetic expression returns True, the value of the first operand expression on the left is returned: true; otherwise the second right-hand operand expression will continue to be detected, and then the value of the second arithmetic expression is returned;

This method is also not favoured because the code on the right side of the operator is not guaranteed to be executed.

General Opposition in | | The right side uses an expression with other functions (assignment, function call, increment or decrease);

The "| |" As an operator of a Boolean algebra, it is simpler and more secure.

(III) "!" Logic is not a unary operator, which is designed to reverse the arithmetic before it is put into operation.

6, assignment operator

(I) "=" is an assignment operator; he always expects the left-hand count to be a variable, an element of an array, or an attribute of an object;

Expecting the right is an arbitrary value of any type;

Right to left binding, if there is more than one assignment operator in an expression, start at the far right.

Note: Each assignment expression has a value, which is the value to the right of the operator;

(II) Assignment operations with operations can be used

The value to the left of "= =" plus the value on the right, assigns the value to the left. The "-=" "/=" "*=" method is the same;

7, other operators

The "?:" conditional operator is the only ternary operator;

Boolean result of an expression 1 (any value of any type): Expression 2 (any value of any type);

Based on the result of the Boolean value of the first operand, if true, executes the second arithmetic expression, returns the value of the second arithmetic expression, and executes a third operand expression that returns the value of the third arithmetic expression if the Boolean value of the first operand evaluates to False.

   third, the JavaScript operator's attention

1, pay attention to the data type passed to the operator and the data type returned! Different operators expect its operand expression to compute a result that conforms to 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".

2, + According to the number of different 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.

3. Note the binding of 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;

JavaScript operators are introduced here, I hope to be helpful to everyone in the daily.
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.