JS in string () and ToString ()

Source: Internet
Author: User

1,. toString ()All of the data can be converted to a string, butexclude null and undefined

For example, to convert false to string type

<script>    var str =  false .toString();    console.log(str,  typeof str); </script>

The returned result is false,string

See if null and undefined can be converted to string JavaScript

<blockquote style= "margin-right: 0px;" dir= "ltr" ><pre  class = "html" name= "code" ><script>    var str =  null .toString();    console.log(str,  typeof str); </script>

Result program Error

<script>    var str = undefined.toString();    console.log(str, typeof str); </script>

Program also error

. toString () parentheses can write a number that represents the binary, corresponding to the binary string

Binary:. toString (2);

Octal:. toString (8);

Decimal:. toString (10);

Hex:. toString (16);

Such as:

var c = 123; Console.log (C.tostring (8)); result 173 2, String ()You can convert null and undefined to a string.

For example, converting null to a string

<script>    var str = String( null );    console.log(str,  typeof str); </script

  

The returned result is null,string

Convert undefined to a string

<script>    var str = String(undefined);    console.log(str,  typeof str); </script>

The returned result is undefined,string

Console.log (String (077)); Returns the result: 63 (if starting with 0 or starting with 0x will be first converted to a binary number, in a string)

JS in string () and ToString ()

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.