Some summaries of implicit conversions in JavaScript

Source: Internet
Author: User

Implicit conversion rules in JavaScript operators:
One, increment decrement operator (front, back)
1. If a valid numeric string or a valid floating-point string is included, the string conversion (number ()) is the numeric value, and then the decrement operation, the type of the return value is: type number.
2. If you do not include a valid numeric string, the value of the string is converted to Nan, and the type of the return value is: Number type.
3. In the case of a Boolean type, True or false is first converted to 1 or 0, and then the addition and subtraction operation, the type of the return value is: Number type.
4. If it is a null type, then NULL is converted to 0, and the type of the return value is: Number type, in addition and subtraction operations.
5. If it is undefined, the undefined is converted to Nan, and then the addition and subtraction operation, the type of the return value is: Number type.
6. In the case of an object, the conversion is made through the object's valueof () method, if the return is Nan, the ToString () method is called, and the type of the return value in the preceding operation is: number type. (Note: An empty array [] returns 0, and an empty object returns Nan if the Add and subtract operation is performed).

The implicit conversion law in logical operators (note: Only undefined, null, NaN, 0, empty strings are converted to false, and the rest is true):
Logical operators are generally used in sentence judgments. The subsequent statement operation is done by judging the value returned by the result.
1. Logical NON (!) Operator: the Boolean () function is first converted to a Boolean value and then reversed.
2. Logic and (&&) operator: If the first value is true after the Boolean () function is converted, the second action value is returned, otherwise the first action value is returned. If there is an action value of NULL this returns NULL, if there is an action value of undefined, then undefined is returned, and Nan is returned if there is a value of Nan.
3. Logic or (| |) Operator: If the first value is converted to False by a Boolean () function, the second action value is returned, otherwise the first action value is returned.
(Note: The operation of the logical operator is a short-circuit logic operation: The previous condition has been able to produce results subsequent conditions are no longer executed!) )

The implicit conversion law of relational operators (the operation value of a relational operator can also be any type):
1. If the two operation values are numeric, the size is directly compared.
2. If the two action values are strings, the strings are compared by their Unicode encoding.
3. If one operation value is numeric, the other value is converted to a number for comparison.
4. If an action value is an object, call the object's valueof () and ToString () methods, and then compare the above.
5. If an action value is a Boolean value, convert the Boolean value to a value and then compare it.
(Note: Nan and any value are not equal, including yourself, and it will return false when compared to any type.) )

The implicit conversion law of equal operator = = and = = =:
1. Boolean, string, and numeric comparisons are converted to numeric values before being compared.
2.null and undefined comparisons are equal, but not congruent.
3.NaN is not equal to any value and will return false.

Some summaries of implicit conversions in JavaScript

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.