The conversion of each data type in JS

Source: Internet
Author: User

The data types in JS are: Object Null Undefined number String Boolean

Convert to Boolean type:boolean ();

  Conversion rules:

    

Data type Value converted to True Value converted to False
Boolean True False
String Any non-empty string "" (empty string)
Number Any non-0 numeric value 0 and Nan
Undefined Any object Null
Object No Undefined

Convert to String type:string () and toString ();

conversion rule:

<script>    var a= "1";     var b= "true";     var c= "null";     var D;    Alert (String (a));     // "1"     alert (String (b));     // "true"     Alert (String (c));         // "NULL"     alert (String (d));     // "undefined"</script>

ToString (); method is not able to convert Null and Undefined

Convert to Floating point:parsefloat ();

  

<script>    var num1 = parsefloat ("1234red")        //1234    var num2 = Parsefloat ("22.5")        //22.5    var num3 = parsefloat ("22.34.5")        //  22.34    var num4 = parsefloat ("0908.4")        //908.4</ Script>

Convert to Integer:parseint ();

<script>    var num1 = parseint ("1234red")    //1234    var num2 = parseint ("")            //NaN    var num3 = parseint ("22.5")        //      var num4 = parseint ("a")        //</script>

Non-numeric conversion to numeric number ();

<script>    var a= "1";    Alert (number (a));     var num1 = number ("Hello world");    // NaN    var num2 = number ("");                // 0    var num3 = number ("0000011");        //  One    var num4 = number ("true");            // 1</script>

     

The conversion of each data type in JS

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.