Javascript 中有 5 種始數據類型
Undefined
Null: 值只有一個 null
Boolean: 值有兩個 true false (var a = false;)
String
Number
1. Undefined: 值只有一個 undefined
<html> <head> <script type="text/javascript"> var s; alert(s); </script> </head> <body> This is my HTML page. <br> </body> </html>
結果: undefined (小寫,表示數據類型 Undefined的實際值)
4. String: 定義時可用單引號,也可用雙引號。(注: js中沒有 char 類型)
判斷一個變量的類型可以用 typeof
typeof 是 一元運算符。后跟變量名稱,用於獲取數據類型。返回值有5個:undefined, string, number, boolean 和 object.
<html> <head> <script type="text/javascript"> var s = "hello"; //s是原始類型 alert(typeof s); </script> </head> <body> </body> </html>
結果: string
如果我們改成
<html> <head> <script type="text/javascript"> var s = new String("hello"); //s是原始類型 alert(typeof s); </script> </head> <body> </body> </html>
結果: Object
Object是所有類型的總稱。