JavaScript Data type conversions

Source: Internet
Author: User

var MyVar = "3.14159";

1. Converting to a string

str = someobj.tostring ()

str = "" + MyVar

2. Converting to integral type

int = ~~myvar or parseint (MyVar)

3. Convert to floating-point type

float = 1*myvar or myVar-0 or parsefloat (MyVar)

4. Convert to Boolean type

Any string with length and any number except 0 is true

BOOL =!! MyVar

In Boolean operations

False, undefineded, NULL, 0, "" Default conversion to False

True, 1, non-empty string, non-null object default conversion to True

5. Converting to an array

Array = [MyVar]

6. Convert to date

The date string must be in the form of YY/MM/DD or Yy/mm/dd HH:mm:ss, and the other form is not possible with the new date (MyVar)

7. Convert to Regular expression

New RegExp (MyVar), creating an expression to use the form of/pattern/flags.

JavaScript Data type conversions

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.