首先,typeof 只能精准的检测基本数据类型,它的返回值就是检测出的数据类型
const res = typeof 123
console.log(res) // number
基本数据类型检测结果
console.log(typeof(123)) // number
console.log(typeof('zz')) // string
console.log(typeof(true)) // boolean
console.log(typeof(undefined)) // undefined
console.log(typeof(null)) // object
以上,当检测 null 的时候被当作空对象,所以结果为 object
第二,typeof() 的返回值是一个 string 类型,所以当两个及以上的 typeof 连用的时候,它的返回值一定是 string 类型
console.log(typeof typeof 123) // string
第三,typeof 要检测的数据类型 和 typeof(要检测的数据类型) 两者的唯一区别就是当 要检测的数据类型在两个及以上的时候要使用 (),否则会按照从左到右的顺序执行,结果可能会出乎意料
const num = 100
const num2 = 200
console.log(typeof num + num 2) // number200
如果检测复杂数据类型,检测结果均为 object