typeof
由于JavaScript是松散类型的,所以提供了typeof来检测数据类型。
typeof({}) // "object"
typeof(95) // "number"
tppeof(NaN) // "number"
typeof(a) // "undefined"
typeof("a") // "string"
typeof(alert) // "function"
var car = null;
typeof(car) // "object"
typeof(undefined) // "undefined"
typeof(true) // "boolean"
相似的兄弟 null和undefined
null == undefined // true
null === undefined // false
根据阮一峰的Blog他说:
null表示“没有对象”该处不应该有值。典型用法是
1.作为函数的参数,表示该函数的参数不是对象
2.作为原型链的终点undefined表示“缺少值”,此处应该有一个值但是还没有定义。典型的用法是
1.变量申明了但是没有赋值
2.调用函数时,应该提供的参数没有提供,那么这个参数就是undefined
3.对象没有赋值的属性,该属性的值为undefined
4.函数没有返回值时,默认返回undefined
我个人的理解就是undefined是“空”而null是“无”