typeof 检测变量的数据类型
typeof 返回6种数据类型:
- object
- undefined
- string
- number
- boolean
- function
var a=null; var b; var c="nihao"; var d=12; var e=true;
console.log(typeof a); //object
console.log(typeof b); //undefined
console.log(b1); //undefined
console.log(typeof c); //string
console.log(typeof d); //number
console.log(typeof e); //boolean
console.log(function(){}); //function
注意:当变量为null时,返回类型是object,这是javascript错误的延续;而变量不存在或者变量未赋值时,返回的是object;