1. 程式人生 > >【JavaScript的五種基本數據類型及轉換】

【JavaScript的五種基本數據類型及轉換】

空字符串 ali 就是 false col eight 字符串 變量的數據類型 輸出

js中有六種數據類型,包括五種基本數據類型(Number,String,Boolean,Null,Undefined),和一種混合數據類型就是特殊的(Object)。

"undefined" 變量未定義
"boolean" 變量是布爾值
"string" 變量是字符串
"number" 變量是數值
"object" 變量是對象或者null
"function" 變量是函數

typeof 操作符可以檢測變量的數據類型(輸出的是一個關於數據類型的字符串)。

1 //          typeof是得到變量的類型(查看數據類型)
2             var
a; //undefined 3 a=null; //object 4 a=0; //number 5 a=NaN; //number 6 a="1"; //string(帶引號的基本都是字符串) 7 a=false; //boolean布爾 8 a=‘‘; //string(帶引號的基本都是字符串) 9 alert(typeof a);

隱式轉換:

以下是一些例子:

其中:

其它類型轉換成數值型:
 數值型+undefined=NaN   

數值型+null=數值
布爾boolean:true+2=3 false+2=2
 1 <!DOCTYPE html>
 2 <html>
 3     <head>
 4         <meta charset="utf-8">
 5         <title></title>
 6     <script type="text/javascript">
 7         /*
 8          if(exp){
 9              exp為true的代碼段;
10          }else{
11 exp為false的代碼段; 12 } 13 */ 14 //其它類型轉換成布爾類型假的有 15 // var a;//undefined->false 16 // typeof得到變量的類型 17 // alert(typeof a); 18 // a=null;//null->false 19 // 0 0.0 NaN->false 20 // a=0; 21 // a=0.0; 22 // a=0/0; 23 // a=NaN; 24 a=null; 25 alert(a); 26 // a=‘‘;//空字符串->false 27 // a=‘0‘; 28 // a=‘ ‘; 29 // alert(typeof a); 30 // if(a){ 31 // alert(‘真‘); 32 // }else{ 33 // alert(‘假‘); 34 // } 35 //其它類型轉換成數值型 36 var b=undefined;//undefined->NaN 37 b=null;//null->0 38 b=false;//true->1 39 // b=false;//false->0 40 // alert(1+b); // 數值型+undefined=NaN 數值型+null=數值 ( boolean:true+2=3 false+2=2) 41 var c=12;//‘12‘->12 42 // c=‘3king‘;//‘3king‘->NaN 43 c=false; 44 // alert(2*c); 45 // c=‘33‘; 46 // alert(typeof c); 47 c=c*1; 48 // alert(typeof c); 49 50 51 </script> 52 </head> 53 <body> 54 <h1>隱式轉換的例子</h1> 55 <script type="text/javascript"> 56 //其它類型轉換成字符串型 57 document.write(undefined);//‘undefined‘ 58 document.write(<br/>); 59 document.write(null);//‘null‘ 60 document.write(<br/>); 61 document.write(NaN);//‘NaN‘ 62 document.write(<br/>); 63 document.write(123);//‘123‘ 64 document.write(<br/>); 65 document.write(true);//‘true‘ 66 document.write(<br/>); 67 document.write(false);//‘false‘ 68 document.write(<br/>); 69 //alert(1+"1");//拼接字符串 70 //alert(‘2‘+12);//拼接字符串 71 </script> 72 </body> 73 </html>

強制轉換:<有五種>

 1 <!DOCTYPE html>
 2 <html>
 3     <head>
 4         <meta charset="utf-8">
 5         <title></title>
 6     <script type="text/javascript">
 7     //其它類型轉換成布爾類型false的有
 8         var test=Boolean(0);
 9         test=Boolean(-0);
10         test=Boolean(NaN);
11         test=Boolean(undefined);
12         test=Boolean(‘‘);
13         test=Boolean(0.0);        
14         test=Boolean(0);
15         //其它類型轉換成字符串型
16         test=String(1234);
17         test=String(23.34);
18         test=String(this is a test);
19         test=String(true);
20         test=String(false);
21         test=String(null);
22         test=String(undefined);
23         test=String(NaN);
24         //其它類型轉換成數值型
25         test=Number(12);
26         test=Number(232.3);
27         test=Number(true);
28         test=Number(false);
29         test=Number(undefined);
30         test=Number(NaN);
31         test=Number(null);
32         test=Number(3king);
33         test=Number(324);
34         //通過parseInt()進行轉換成整型
35         test=parseInt(123);
36         test=parseInt(222,3);        //234
37 //        alert(test);
38         test=parseInt(0xabcdef);    //11295375
39         //alert(test);
40         test=parseInt(012344);    //12344
41 //        alert(test);
42         test=parseInt(45,16);        //69
43 //        alert(test);
44         test=parseInt(3ki23ng);    //3
45 //        alert(test);                   //3
46         test=parseInt(true);        //NaN
47         //alert(test);
48         test=parseInt(true);        //NaN
49         //alert(test);
50         test=parseInt(  35  6 a );//35
51 //        alert(test);
52         //通過parseFloat()轉換成浮點型
53         test=parseFloat(123.34abc);
54         //test=parseFloat(‘123‘);
55     //    test=parseFloat(‘sdf‘);
56 //        test=parseFloat(‘ 2e3a‘);
57         alert(test);
58     </script>
59     </head>
60     <body>
61     <h1>強制轉換的例子</h1>
62     </body>
63 </html>

【JavaScript的五種基本數據類型及轉換】