String
:任意字符串Number
:任意的數字boolean
:true
/false
null
:null
undefined
:undefined
Symbol
: ES6新增,表示獨一無二的值Object
:任意對象Array
:一種特別的對象(數值下標,內部數據是有序的)Function
:一種特別的對象(能夠執行)typeof
:返回數據類型的字符串表達var a console.log(a) // undefined console.log(typeof a) // "undefined" console.log(a === undefined) // true console.log(typeof a === undefined) // false console.log(typeof a === "undefined") // true console.log(undefined === "undefined") // false a = 4 console.log(typeof a) // "number" console.log(typeof a === Number) // false console.log(typeof a === "number") // true a = "hahha" console.log(typeof a) // "string" a = false console.log(typeof a) // "boolean" a = null console.log(typeof a) // object console.log(a === null) // true
注意:typeof
返回的是數據類型的字符串表達形式。javascript
typeof true //"boolean" typeof "hahha" //"string" typeof 12 //"number" typeof null //"object" typeof ccc //"undefined" typeof function(){} //"function" typeof {} //"object"
instanceof
:類型的實例
首先要理解
instanceof
的含義:
instance
是例子的意思,A instanceof B
其實是判斷A
是不是B
的一個實例。理解了這一點,就不難判斷類型了。var b1 = { b2: [1, "hehe", console.log], b3: function () { console.log("b3") return function () { return "Mandy" } } } console.log(b1 instanceof Object) // true console.log(b1.b2 instanceof Array, b1.b2 instanceof Object) // true true console.log(b1.b3 instanceof Function, b1.b3 instanceof Object) //true true console.log(typeof b1.b2) // "object" console.log(typeof b1.b3) // "function" console.log(typeof b1.b2[1]) // "string" console.log(typeof b1.b2[2]) // "function" b1.b2[2](555) // 555 console.log(b1.b3()()) // "b3" "Mandy"
注意:java
Function
類型,也是 Object
類型Array
類型,也是 Object
類型===
undefined
和 null
ccc === "undefined" // true null === null // true
typeof
:數組
undefined
/ 數值 / 字符串 / 布爾值 / function
不能判斷 null
與 object
, array
與 object
函數
typeof null // "object" typeof [] // "object"
instanceof
:ui
A instanceof B
===
:this
undefined
, null
undefined
與 null
的區別?undefined
表明定義了,未賦值null
表明定義了,而且賦值了,只是賦的值爲 null
// undefined與null的區別? var a console.log(a) // undefined a = null console.log(a) // null
null
?typeof null === "Object"
//起始 var b = null // 初始賦值爲null, 代表將要賦值爲對象 //肯定對象就賦值 b = ['atguigu', 12] //最後 b = null // 讓b指向的對象成爲垃圾對象(被垃圾回收器回收)
數據的類型:code
變量的類型(變量內存值的類型)對象
// 實例: 實例對象 // 類型: 類型對象 function Person (name, age) {// 構造函數 類型 this.name = name this.age = age } var p = new Person('tom', 12) // 根據類型建立的實例對象