1.typeof ---直接返回数据类型
console.log(typeof 1); //number
console.log(typeof true); //boolean
console.log(typeof '1'); //string
console.log(typeof []); //object
console.log(typeof {}); //object
console.log(typeof function(){}); //function
console.log(typeof Date); //function
console.log(typeof null); //object
console.log(typeof undefined); //undefined
缺点:不能准确判断数据类型
2.instanceof ---返回true/false
console.log(1 instanceof Number); //false
console.log("1" instanceof String); //false
console.log(true instanceof Boolean); //false
console.log([] instanceof Object); //true
console.log([] instanceof Array); //true
console.log({} instanceof Object); //true
console.log(function(){} instanceof Function); //true
console.log(Date instanceof Function); //true
console.log(null instanceof Object); //false
console.log(undefined instanceof Object); //false
instanceof用来判断一个对象是否是对象类型的实例,所以判断1,“1”,true等值时返回false
console.log(new Number(1) instanceof Number);
console.log(new String("1") instanceof String);
console.log(new Boolean(true) instanceof Boolean); 实例化之后返回true
3.constructor
4.Object.prototype.toString.call()
console.log(Object.prototype.toString.call(1)); [object Number]
console.log(Object.prototype.toString.call("1")); [object String]
console.log(Object.prototype.toString.call(true)); [object Boolean]
console.log(Object.prototype.toString.call([])); [object Array]
console.log(Object.prototype.toString.call({})); [object Object]
console.log(Object.prototype.toString.call(Date)); [object Function]
console.log(Object.prototype.toString.call(function(){})); [object Function]
console.log(Object.prototype.toString.call(null)); [object null]
console.log(Object.prototype.toString.call(undefined)); [object undefined]
可以准确判断数据类型