Introduction to operators and rules in JavaScript _ basic knowledge

Source: Internet
Author: User
There are many operators in JavaScript, which are mainly divided into Arithmetic Operators, equivalent full-Union operators, comparison operators, string operators, logical operators, and value assignment operators. They all have their own operation rules, this article introduces many operators in JavaScript, including Arithmetic Operators, equal all operators, comparison operators, string operators, logical operators, and value assignment operators. These operators all have their own operation rules. The following describes the operators in JavaScript.

   I. Types of JavaScript Operators

1. Arithmetic Operators: +,-, *,/, %,-(inverse of one dollar), ++, and ,--

2. Equivalent operators: =, = ,! = ,! ===

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

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

5. logical operators: &, | ,! ,

6. assignment operators: =, + =, * =,-=,/=

   Ii. JavaScript operator rules

1. Arithmetic Operator rules

"+": You can perform addition and connection operations. If one of the two operators is a string, javascript converts the other to a string, connect the two operation numbers.

"+": If an operation number is an object, javascript converts the object to a number for addition or string join operations;

"-" "*" "/" If one of the two operation numbers is a non-number, it is converted to a number for mathematical operation.

"/" In javascript, because all the numbers are floating-point numbers, the division result is floating-point numbers, 5/2 = 2.5; the result of Division 0 is positive and negative infinity; 0/0 is NaN;

"%" Modulo operator: calculates the modulus of the first operation number to the second operation number, that is, the remainder is returned when the first operation number is divided by the second operation number. If the number of operations is non-numeric, it is converted to a number.

"-": Returns the number of operations. If the number is not a number, it is converted to a number.

"++" "--" Incremental operator/subtraction operator number: the number of operations must be a variable, an element of a tree, or an attribute of an object. If the number is not a number, convert it to a number.

NOTE: If "+ +" is located before the operation number, increment the operation number first, and then calculate the value after the operation number increases.

If "--" is after the number of operations, calculate the value before the number of operations does not increase, and then increment the number of operations.

For example, I = 1; // assign a value to I 1.

J = ++ I; // increment I to 2 first, and then assign 2 to j. The value of I is 2, and the value of j is 2.

I = 1; // assign a value to I value 1

J = I ++; // assign an I value to j first, then increment the I value to 2, the I value is 2, and the j value is 1.

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

2. Equal operators and full Operators

(I) "=" "! = "Equivalent operator and non-equivalent OPERATOR:

Compare two operation numbers and return a non-Boolean value.

Comparison values, strings, and boolean values use values. They are equal only when the values of the two variables are equal.

When comparing objects, arrays, and functions, we use references. When two variables reference the same object, they are equal. Two different arrays are completely different, even if they have identical elements. For variables that store objects, arrays, and function references, they are equal only when they reference the same object, array, and function.

Note! Principles to be followed:

When the two operation numbers are of different types: convert them to the same type,

1) A number is compared with a string that is converted to a number.

2) convert true to 1 and false to 0 for comparison.

3) an object, array, function, and a number or string, object, array, and function are converted to original type values, and then compared. (Use valueOf first. If not, use toString)

4) Other types of combinations are not stars.

After the two operation numbers are of the same type or converted to the same type:

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

2) two numbers: the two numbers are the same. If one is NaN or both are NaN, they are different.

3) if both are true, or both are false, the same is true.

4) if the two references the same object, function, and array, they are equal. If they are not referenced by the same object, function, or array, they are different, even if the two objects, functions, and arrays can be converted to completely equal original values.

5) Two null or two are undefined, so they are equal.

"! = "Non-equivalent OPERATOR: The result is opposite to that detected by the equivalent operator.

(II) "=" "! ===" All-in-One and non-all-in-one operator numbers:

Identical operators follow the comparison rules of equal operators. However, they do not perform type conversion on the number of operations. If the two numbers are of different types, false is returned; only when the two operation numbers are of the same type can they be compared according to the comparison rules of equal operators.

"! = "The result of comparison between non-all-same operators and all-same operators is the opposite. Returns true if the two operation numbers have different types or values.

3. Comparison Operators

These comparison operators are used to compare different types of values, and a Boolean value is returned.

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

Note: The number of computation operations can be any type, but can only be performed on numbers and arithmetic operations. The number of operations that are not a number or string will be converted to a number or string.

1) if both operations are numbers, or both operations can be converted to numbers, compare them according to the number size;

2) if both operations are strings, or both operations can be converted to strings, the operations will be compared alphabetically;

3) if a string encounters a number, convert the string to a number for comparison.

4) if the number of operations cannot be converted to a number or a string, false is returned.

4. String Operators

There is no dedicated string operator, but some operators differ in the number of string operations.

(I) "+" connects two strings;

1) When both operators are strings, they are connected;

2) when one of them is a number, convert the number into a string and connect it;

(II) Comparison operators such as ">" confirm the order of the two strings by comparison, which adopts the Character Sequence. smaller operators are located at the front of the upper limit, and upper case letters are located before lower case letters.

(III) "+" depends on the computing order,

For example, if s = 1 + 2 + "var", 3var is returned, because 1 + 2 is calculated first, and result 3 is converted into a string connected to "var;

For example, if s = "var" + 1 + 2, the returned result var12 is returned. Because the connection between var and 1 is calculated first, var1 is connected to 2 converted to a string.

5. logical operators

Used to execute Boolean operations, which are often used together with comparison operators to represent complex comparison operations.

"&" Logic and operation, "|" logic or operator, "!" Logical non-Operator

(I) "&" when both operators are boolean values, logic and perform the and operation on them, that is, when and only when both boolean values are true, returns true; otherwise, returns false.

Note: Actual Performance

"&" Checks the Boolean value of the first expression. If the first expression returns false, the value of the first expression on the left is returned: false; otherwise, the second arithmetic expression on the right will be detected, and the value of the second arithmetic expression will be returned;

For example, if (a = B) stop (); is equivalent to (a = B) & stop ();

This method is not supported because the code on the right side of the operator cannot be executed,

For example, if (a <B) & (B ++ <10) stop (); if the increment on the right of the operator is required, but it may not be executed because of the previous return value. It is generally opposed to the use of expressions with other functions (Value assignment, function call, increase or decrease) on the Right );

It is simpler and safer to regard "&" as a Boolean algebra operator.

(II) "|" when both operators are boolean values, the logic or performs the or operation on them, that is, when either of the two boolean values is true, returns true; otherwise, returns false.

Note: Actual Performance

"|" Checks the Boolean value of the first expression. If the first expression returns true, the value of the first expression on the left is true; otherwise, the second arithmetic expression on the right will be detected, and the value of the second arithmetic expression will be returned;

This method is also not supported because the code on the right side of the operator cannot be executed,

It is generally opposed to the use of expressions with other functions (Value assignment, function call, increase or decrease) on the right of | );

It is simpler and safer to regard "|" as a Boolean algebra operator.

(III) "!" The logic is not a unary operator. It is placed before the number of operations to reverse the number of operations.

6. Value assignment operator

(I) "=" is a value assignment operator. He always expects the number of operations on the left to be a variable, an element of an array, or an attribute of an object;

It is expected that the right side is an arbitrary value of any type;

The combination from right to left. If an expression has multiple value assignment operators, the calculation starts from the rightmost.

Note: Each value assignment expression has a value, that is, the value on the right of the operator;

(II) value assignment with operation can be used.

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

7. Other operators

"? : "The conditional operator is a unique ternary operator;

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

Based on the Boolean value of the first operation, if true, the second operation number expression is executed and the value of the second operation number expression is returned; if the Boolean value of the first operation is false, the third operation number expression is executed and the value of the third operation number expression is returned.

   Iii. JavaScript Operators

1. Pay attention to the Data Type passed to the operator and the data type returned! Different operators expect that the result calculated by the number expression is of a certain data type.

For example, "a" * "B" is invalid for multiplication operations on strings. However, if possible, javascript converts the expression to the correct type. Therefore, the expression "3" * "5" is valid. javascript converts the string to a number and returns the number 15, instead of the string "15 ".

2. + the performance varies depending on the number of operations:

String + String = string (connected); "a" + "B" = "AB" 5 "+" 6 "=" 11"

String + digit = (string converted to digit) string (connected); "a" + 5 = "a5" 5 converted to string "1" + 0 = "10"

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

3. Pay attention to the concatenation of operators. Some operators are condensed from left to right, and some are condensed from right to left.

For example, w = a + B + c is equivalent to w = (a + B) + c;

W = --- B is equivalent to w =-(-B); w = a = B = c is equivalent to w = (a = (B = c ))

The combination of unary operators, value assignment operators, and ternary operators is from right to left;

The JavaScript operators are introduced here, hoping to help you in your daily work.
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.