JavaScript cast-in-depth explanation

Source: Internet
Author: User

Convert to Numeric

number function cast to numeric value

Value --Convert to original

string -if it can be resolved to a numeric value, convert to a numeric value; otherwise convert to NaN or 0

True->1,false->0

Undefined->nan

Null->0

Convert to integral type

Praseint ()

Convert to floating point type

Prasefloat ()

Attention

the number function is much stricter than the Praseint function to convert a string to a numeric value . Basically, as long as one character cannot be converted to a number, the entire string is converted to NaN

Convert to String

through string functions converted to strings

numeric value itself

String, string itself

True-> "true",false-> "FalSe"

undefined-> "Undefined"

Null-> "NULL"

Convert to String type

ToString ()

Convert to Boolean type

through Boolean function cast to Boolean value

0,-0->false

Nan->false

empty string ->false

Undefined->false

Null->false

<! DOCTYPE html>

<meta charset= "Utf-8" >

<title></title>

<script type= "Text/javascript" >

other types that convert to Boolean type falSe have

var test=boolean (0);

Test=boolean (-0);

Test=boolean (NaN);

Test=boolean (undefined);

Test=boolean (");

Test=boolean (0.0);

Test=boolean (' 0 ');

other types convert to string type

Test=string (1234);

Test=string (23.34);

Test=string (' This is a teSt ');

Test=string (TRUE);

Test=string (falSe);

Test=string (NULL);

test=string (undefined);

Test=string (NaN);

other types converted to numerical type

Test=number (12);

Test=number (232.3);

Test=number (TRUE);

Test=number (falSe);

Test=number (undefined);

Test=number (NaN);

Test=number (NULL);

Test=number (' 3king ');

Test=number (' 324 ');

convert to integral type by parseint ()

Test=parseint (' 123 ');

Test=parseint (' 234 ', 0);

Test=parseint (' 0xabcdef ');

Test=parseint (' 012344 ');

Test=parseint (45,16);

Test=parseint (' 3ki23ng ');

Test=parseint (' true ');

Test=parseint (TRUE);

Test=parseint (' 6 a ');

convert to floating-point by parsefloat ()

Test=parsefloat (' 123.34abc ');

Test=parsefloat (' 123 ');

Test=parsefloat (' Sdf ');

Test=parsefloat (' 2e3a ');

alert (teSt);

</Script>

<body>

examples of

</body>

Operation Result:

original link:http://www.maiziedu.com/wiki/js/mandatory/

JavaScript cast-in-depth explanation

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.