JavaScript data types
There are 6 different data types in JavaScript:
- string
- number
- boolean
- object
- function
- Symbol
3 types of objects: - Object
- Date
- Array
2 data types that do not contain any values: - null
- undefined
typeof operator
You can use the typeof operator to view the data types of JavaScript variables.
Examples:
typeof "John" // 返回 string
typeof 3.14 // 返回 number
typeof NaN // 返回 number
typeof false // 返回 boolean
typeof [1,2,3,4] // 返回 object
typeof {name:'John', age:34} // 返回 object
typeof new Date() // 返回 object
typeof function () {} // 返回 function
typeof myCar // 返回 undefined (如果 myCar 没有声明)
typeof null // 返回 object
caution:
- The data type of NaN is number
- The data type of Array is object
- The data type of Date is object
- The data type of null is object
- The data type of undefined variables is undefined
If the object is JavaScript Array or JavaScript Date , we cannot judge their type by typeof, because they all return object .