typeof 只能判断出基本数据类型。对应复杂的数据类型它就无能为力,如下面判断的数组和对象返回的都是object
基本数据类型(五种)——分别为number、 string、boolean、null、undefined
//简单数据类型
let str = '123';
let nub = 123;
let bool = false;
let null1 = null;
let undefined1;
console.log(typeof(nub));//number
console.log(typeof(str));//string
console.log(typeof(bool));//boolean
console.log(typeof(null1));//null默认object
console.log(typeof(undefined1));//undefined
toString
// 复杂数据类型
let obj = {};
let arr = [];
console.log(Object.prototype.toString.call(obj));//[object Object]
console.log(Object.prototype.toString.call(arr));//[object Array]