Javascript强制转换

  Javascript强制转换强制转换一共有五种转换方式,各有各的用处,希望大家在实际的使用中灵活运用,不要死板。

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title></title>
 6 </head>
 7 <body>
 9     <script>
10         // 其他类型转化为布尔型
11         test=Boolean(0);
12         test=Boolean(-2);
13         test=Boolean(NaN);
14         test=Boolean('');
15         test=Boolean('0.0');
16         alert(test);
17         alert(typeof test);
18     </script>
19     <script>
20         //其他类型转化为字符串
21         test=String(123);
22         test=String(123.0);
23         test=String(123.2);
24         test=String(true);
25         alert(test);
26     </script>
27     <script>
28         //其他类型转化为数值型
29         test=Number('12');
30         test=Number(true);
31         test=Number(false);
32         test=Number('false');
33         test=Number(undefined);
34         alert(test);
35     </script>
36     <script>
37         //其他类型化为整型
38         test=parseInt(123.22);
39         test=parseInt(NaN);
40         test=parseInt('123ae');
41         test=parseInt('123e2,10');
42         alert(test); // string 以 "0x" 开头,parseInt() 会把 string 的其余部分解析为十六进制的整数。
43     </script>
44 </body>
45 </html>

 

posted on 2018-01-07 19:42  Cc_Pz  阅读(236)  评论(0编辑  收藏  举报