数据类型判断
# 数据类型判断
判断原始类型数据
例如Number,String,Boolean,undefined类型
可以使用
typeof进行判断typeof 12 //'number' typeof true //'boolean' typeof '12' //'string' typeof undefined //'undefined'在使用
typeof判断null时会返回对象,这是js底层的历史遗留问题对于
null类型的判断,直接===判断即可let a=null; a===null; //true你也可以使用
typeof判断一个函数的类型,但通常不推荐这么做typeof function(){} //'function'
判断引入类型的方式
使用
Object.prototype.toString.call();方法进行判断Object.prototype.toString.call([]); //'[object Array]' Object.prototype.toString.call({}); //'[object Object]' Object.prototype.toString.call(function(){}); //'[object Function]' Object.prototype.toString.call(new Error()); //'[object Error]' Object.prototype.toString.call(null); //'[object Null]'
编辑 (opens new window)
上次更新: 2022/05/28, 10:34:23