JavaScript 中有五種可包含值的數據類型:
- 字符串(string)
- 數字(number)
- 布爾(boolean)
- 對象(object)
- 函數(function)
有三種對象類型:
- 對象(Object)
- 日期(Date)
- 數組(Array)
同時有兩種不能包含值的數據類型:
- null
- undefined
- typeof 運算符
-
您可以使用 typeof 運算符來確定 JavaScript 變量的數據類型。
實例
typeof "Bill" // 返回 "string" typeof 3.14 // 返回 "number" typeof NaN // 返回 "number" typeof false // 返回 "boolean" typeof [1,2,3,4] // 返回 "object" typeof {name:'Bill', age:62} // 返回 "object" typeof new Date() // 返回 "object" typeof function () {} // 返回 "function" typeof myCar // 返回 "undefined" * typeof null // 返回 "object"