JS的typeof力所能及已经力所不及
typeof返回值列表

typeof的作用
返回参数的类型
typeof能判断的类型
- String类型:
typeof 'a' === 'string' - Undefined类型:
typeof undefined === 'undefined' - Boolean类型:
typeof true === 'boolean' - Number类型:
typeof 22 === 'number' - Symbol类型:
typeof Symbol() === 'symbol' - Function类型:
typeof function(){} === 'function'
typeof不能判断的类型
- Null类型:
typeof null === 'object' - Array类型:
typeof [] === 'object' - 除去Array/Function类型的对象:
typeof {} === 'object'
typeof不能判断的类型,该如何去判断?
- Null类型的值有且仅有一个值
null因此可以通过值比较进行判断:
const a = null
if (a === null) {
console.log('a是Null类型')
} else {
console.log('a不是Null类型')
}
- 判断是否是数组
Object.prototype.toString.call([]) === '[Object Array]'
- 判断是否是普通对象
Object.prototype.toString.call({}) === '[Object Object]'
如果发现任何错误,请评论指正,谢谢!

浙公网安备 33010602011771号