typeof运算符: 判断一个变量的类型
let num = 10
let str = 'abc'
let bol = true
let und = undefined
let nul = null
let syb = Symbol('&')
let bigInt = 123n
let arr = [10, 20, 30]
let fn = function () { }
let obj = { name: '张三' }
let date = new Date()
console.log(typeof num)//number
console.log(typeof str)//string
console.log(typeof bol)//boolean
console.log(typeof und)//undefined
console.log(typeof nul)//object
console.log(typeof syb)//symbol
console.log(typeof bigInt)//bigInt
console.log(typeof arr)//object
console.log(typeof fn)//function
console.log(typeof obj)//object
console.log(typeof date)//object
不难发现 typeof 只能检测基础数据类型和函数,其他的都是返回 Object
Object.prototype.toString.call()方法:万能检测数据类型
let num = 10
let str = 'abc'
let bol = true
let und = undefined
let nul = null
let syb = Symbol('&')
let bigInt = 123n
let arr = [10, 20, 30]
let fn = function () { }
let obj = { name: '张三' }
let date = new Date()
console.log(Object.prototype.toString.call(num))//[object Number]
console.log(Object.prototype.toString.call(str))//[object String]
console.log(Object.prototype.toString.call(bol))//[object Boolean]
console.log(Object.prototype.toString.call(und))//[object Undefined]
console.log(Object.prototype.toString.call(nul))//[object Null]
console.log(Object.prototype.toString.call(syb))//[object Symbol]
console.log(Object.prototype.toString.call(bigInt))//[object BigInt]
console.log(Object.prototype.toString.call(arr))//[object Array]
console.log(Object.prototype.toString.call(fn))//[object Function]
console.log(Object.prototype.toString.call(obj))//[object Object]
console.log(Object.prototype.toString.call(date))//[object Date]
instanceof运算符: 用于判断某个函数是否被另一个函数构造
let arr = [10, 20, 30]
let fn = function () { }
let obj = { name: '张三' }
let date = new Date()
console.log(arr instanceof Array)//true
console.log(fn instanceof Function)//true
console.log(obj instanceof Object)//true
console.log(date instanceof Date)//true