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