2>typeof 和 instanceof 操作符

99 阅读1分钟

1> typeof

typeof是判断一个变量是否为str, Number, Boolean, undefined的最好方式

let str = 'str'
let num = 10
let flag = true
let u
let n = null 
console.log( typeof str)  // string
console.log( typeof num)  // Number
console.log( typeof flag)  // Boolean
console.log( typeof u)  // undefined
console.log( typeof n)  // Object

2> instanceof

instanceof判断是一个什么类型的对象

let arr = [1, 2, 3, 4]
let obj = {text:'文字'}
console.log(arr instanceof Array)  // true
console.log(obj instanceof Object)  // true