Conversion of JS type to string and Boolean type

Source: Internet
Author: User

In the previous article, we talked about how to turn a numeric type, and today summarizes the methods for turning strings and Boolean types:

The main methods of the string transfer are:

ToString ();

String ();

The specific usage is shown in the following table:

Method Example return value Description
ToString ():
Undefined and null do not have the ToString () method
[' 1 ', ' 2 ', ' 3 '].tostring () The Converts the element of an Array to a string. The resulting strings are separated by commas and joined together.
True.tostring () "True" Returns "true" if the Boolean value is true. Otherwise, return "false".
var num = 111;
Console.log (Num.tostring ());
"111" Numeric type to String type
String () string () converts null and undefined to a string, but cannot be converted to a string.
String (NULL), which returns "null".
String (undefined), which returns "undefined".


The main types of turn Boolean methods are:

Boolean ():

The specific usage is shown in the following table:

Method Example return value Description
Boolean ():
Convert any type of value to a Boolean value
Boolean (False) False False, "", 0, NaN, null, undefined, and any other value will be converted to true.
Boolean ("") False
Boolean (0) False
Boolean (NaN) False
Boolean (undefined) False
Boolean ("false") True
The string type "false" turns to a Boolean type of false:
var value = "false";
var flag = value = = = "False"? False:true;

Conversion of JS type to string and Boolean type

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.