// 数据类型的判断 var num=123; var bool=true; var str='qwe'; var nu=null; var und=undefined; var arr=[]; var obj={}; // typeof('表达式') // typeof '变量名' console.log(typeof(num));//number console.log(typeof bool);//boolean console.log(typeof str);//string console.log(typeof nu);//object console.log(typeof und);//undefined console.log(typeof arr);//object console.log(typeof obj);//object // 需要注意,在使用typeof时,判断null和Array的结果都为object // 判断对象和数组 console.log(arr.length)//0 console.log(obj.length)//undefined console.log(arr instanceof Array)//true console.log(obj instanceof Object)//true console.log(Object.prototype.toString.call(arr))//[object Array] console.log(Object.prototype.toString.call(obj))//[object Object] console.log(arr.constructor===Array);//true console.log(obj.constructor===Object);//true
代码截图: