typeof与instanceof比较+undefined与null各种值的相互比较
1、typeof:返回一个字符串 根据typeof判断对象 表达式 返回值 typeof undefined 'undefined' typeof true 'boolean' typeof 123 'number' typeof "abc" 'string' typeof function() {} 'function' typeof {} 'object' typeof [] 'object' typeof null 'object' function f(...args) { console.log(typeof args); //object console.log(args instanceof Array); //true} 总结:typeof返回值是一个字符串,该字符串说明运算数的类型,一般只返回一下六种: number、string、Boolean、undefined、function、object(null、对象、数组) 对于数组、对象以及null,返回值都为object,这正是typeof的局限性。 2、instanceof:用于判断一个变量是否为某个类的实例 var a = []; console.log(a instanceof Array); //true console.log(a instanceof Object); //true