Js learning notes (4) expressions and operators

Source: Internet
Author: User

1. If you do not know the operator priority, the simplest way is to use parentheses to clearly indicate the computing order.

The assignment operation has a very low priority and is executed almost always at the end.

2. If one of the "+" (plus sign) operator expressions is a string, but the other is not, the other is automatically converted to a string;

If a number of operations in the plus sign operator is an object, the object is converted to a number that can be used for addition operations or a string that can be used for join operations, this conversion is implemented by calling the valueof () or tostring () method of the object.

3. If the number of subtraction operators is not a number, the system automatically converts them to numbers.

That is to say, the number of plus signs is converted to a string first, and the number of minus signs is converted to a number first. Similarly, the number of operators that can only perform numeric operations is converted to a number. (Comparison operators are converted to numbers for Comparison first)

4. the plus sign operator can convert a parameter to a number. If it cannot be converted to a number, NaN is returned.

For example, var a = "100"; var B = + a at this time, the value of B is 100

5. The increment operator (++) can only be used for variables. If it is used before a variable, it is the first increment operator. If it is used after a variable, it is the second increment operator. The pre-increment operator uses the post-increment value for calculation, and the post-increment operator uses the pre-increment value for calculation, such:

Var a = 1

Var B = a ++ // at this time, the value of B is 1, because the value of B is the value before a increments. But the value of a is already 2,

Var a = 1

Var B = ++ a // at this time, the value of B is 2, and the value of a is 2.

The usage of the reduce operator (--) is the same as that of the increment operator.

6. For (=), to establish an equation, the following conditions must be met:

  • The equations have the same value and type.
  • If both sides of the equation are referenced variables of the type, such as arrays, objects, and functions, make sure that both sides reference the same object. Otherwise, even two identical objects are not completely equal.
  • The values on both sides of the equation are null or undefined, but if it is NaN, it will not be equal.

7. For (=), to establish an equation, the following conditions must be met:

  • The equations have different types, but the values are the same after automatic conversion. If one side of the equation is a number, the non-numeric type on the other side is converted to the numeric type first; A boolean value is always converted to a number for comparison. true is converted to 1 and false to 0 regardless of the number type on both sides of the equation. The object will also be converted.
  • Null = undefined

8. Comparison operators such as greater than or less than can only compare numbers or strings. If they are not numbers or strings, they will be converted to numbers or strings. If both a string and a number exist, the string is first converted to a number. If it cannot be converted to a number, it is converted to NaN. At this time, the final result of the expression is false. If an object can be converted to a number or string, it is converted to a number first. If neither operation count can be converted to a number or string, the result is false. If one of the arithmetic values is NaN or is converted to NaN, the result of the expression is always false. when two strings are compared, the characters are compared one by one based on the numbers in the Unicode Character Set. Therefore, the Case sensitivity of letters also affects the comparison results.

9. The in operator requires that the number of operations on the left be a string or can be converted to a string. The number of operations on the right is an object or an array, if the value on the left is an attribute name of the right object, true is returned.

10. The intanceof operator requires that the number of operations on the left be an object, and the number of operations on the right be the name of the object class. If the object on the left of the operator is an instance of the right class, true is returned. In js, the object class is defined by the constructor, so the number of operations on the right should be the name of a constructor. Note that all objects in js are Object class instances.

11. When the + operator is used for numbers or strings, it may not be converted into strings for connection. For example:

Var a = 1 + 2 + "hello" // The result is 3 hello

Var B = "hello" + 1 + 2 // The result is hello12.

The reason for this is that the + operator operates from left to right.

12. Because (=) is defined as an operator, it can be applied to more complex expressions. For example:

(A = B) = 0 // assign a value to B first, and then check whether the value of a is 0.

The combination of the value assignment operator is from right to left, so it can be used as follows:

A = B = c = d = 100 // assign the same value to multiple variables

13. typeof is also an operator used to return the type of operation number. typeof can also be used to enclose the operation number with parentheses. Typeof is used only to differentiate between objects and the original data type.

14. delete is a unary operator used to delete the object attributes, array elements, or variables specified by the number of operations. If deletion is successful, true is returned, and false is returned if deletion fails. Not all attributes and variables can be deleted. For example, variables declared with var cannot be deleted, and internal core attributes and client attributes cannot be deleted. Note that when you use delete to delete a non-existent attribute (or when its operation number is not an attribute, array element, or variable ),True.

Delete only affects the attribute or variable name, and does not delete the object referenced by the attribute or variable (if the attribute or variable is of a reference type)

15. void is also a mona1 operator, which ignores the number of operations and returns undefined

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.