typeof
检测变量的数据类型
typeof "John" // 返回 string
typeof 3.14 // 返回 number
typeof false // 返回 boolean
typeof [1,2,3,4] // 返回 object
typeof {name:'John', age:34} // 返回 object
在JavaScript中,数组是一种特殊的对象类型。 因此 typeof [1,2,3,4] 返回 object。
null
在js中,null表示“什么都没有”,表示一个空对象引用。
主动释放一个变量引用的对象,表示这个变量没有不再指向任何对象地址
用 typeof 检测 null 返回是object。
var person = null; // 值为 null(空), 但类型为对象
undefined
在 JavaScript 中, undefined 是一个没有设置值的变量。
var person; // 值为 undefined(空), 类型是undefined
person = undefined; // 值为 undefined, 类型是undefined
typeof 一个没有值的变量会返回 undefined。
undefined 和 null 的区别
null 和 undefined 的值相等,但类型不等:
typeof undefined // undefined
typeof null // object
null === undefined // false
null == undefined // true