JS type conversion

Source: Internet
Author: User

First, JS data type

Raw data type: Null number String Undefined Boolean symbol (denotes unique value) about Symbol https://www.jianshu.com/p/174f88d189c6

Reference type Object

Second, type conversion

1.Number function

    • Original type
    1. String: Converts to a number if it can be resolved to a number, otherwise Nan, an empty string is converted to 0
    2. Boolean value: True to convert to 1,false to 0
    3. null:0
    4. Undefined:nan
    • Reference type: Object

The valueof method is called first, and if the method returns data of the original type, the number conversion is called directly;

If the ValueOf method returns a value of the reference type and then calls the object's ToString method, if the ToString method returns the data of the original type, then the number is converted;

If the ToString method returns a reference type, an error occurs.

2.String function

    • Original type
    1. Value: A string converted to a response
    2. Boolean: True converts to "true", false to "false"
    3. Null: Converted to "null"
    4. Undefined: "Undefined"
    • Reference type: Object

The ToString method is called first, and if the method returns data of the original type, the string conversion is called directly;

If the ToString method returns a value of a reference type, then the ValueOf method is called, and if the ValueOf method returns the data of the original type, then the string is converted;

If the ValueOf method returns a reference type, an error occurs.

3.Boolean function

Undefined null-0 +0 "(empty string) NaN is converted to false, the rest is true

Interesting topics:

JS type conversion

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.