Data type conversions in JavaScript

Source: Internet
Author: User
Tags true true

1. Convert other data types to number type

Number (), strict

Parsefloat/parseint-Not strict

1) Convert the Boolean type to a number true->1 false->0

2) Convert null/undefined to digital null->0 Undefined->nan 10+null=10 10+undefined=nan

3) Convert [] to a number first call the array's ToString method to put []-> "", and then use the numbers method to put "" "->0

2. Convert other data types to Boolean types

  Only 0, NaN, null, Undefined, "" These five are converted to Boolean types when false, the rest of the conversion is true

3, two values to compare, if the left and right side of the data type is different, we need to follow a set of rules for the default data type conversion

1) Object = = Object compares the memory address stored by itself, not equal if the memory address is different []==[]->false each empty array is a separate memory address

2) Object = = String called the ToString method to convert the object to a string []== ""->true

3) Object = = Boolean convert to Number (Tostring-number), and Boolean to number (true->1 false->0)

[]==false->true []->0 false->0 0==0->true

! []==false! [] First convert an empty array to a Boolean type and then take the inverse->false false==false->true

4) Object = = Number object converted to digital []==0->true []==1->false

5) String = = Boolean both converted to the number "3" ==true->false "1" ==true->true

6) String = = number String converted to number "" ==0->true

7) Boolean = = Numeric Boolean converted to digital true==1->true True==2->false

8) null==undefined->true null===undefined->false

Null/undefined and any other values are not equal null==0->false

9) Nan==nan->false NaN and any values (including yourself) are not equal

Data type conversions in JavaScript

Related Article

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.