基本类型:number, string, boolean, undefined, null, symbol
复合类型:对象
判断类型的几种方式
typeof
typeof 1 // numbertypeof 's' // stringtypeof true // booleantypeof undefined // undefinedtypeof null // object // 重点记: 这是一个 js 的 bug *typeof NaN // number *const v1 = {}const v2 = []const v3 = function () {}const v4 = () => {}const v5 = Symbol('s')const v6 = new Boolean(true)const v7 = Number('w')typeof v1 // objecttypeof v2 // objecttypeof v3 // function *typeof v4 // function *typeof v5 // symboltypeof v6 // object *typeof v7 // number *
typeof a // ?typeof b // ?var a = 1let b = '1'
instanceof
1 instanceof Number // false
new Number(1) instanceof Number // true
Number(1) instanceof Number // false
Boolean(true) instanceof Boolean // false
new Boolean(false) instanceof Boolean // true
null instanceof Object // false
[] instanceof Object // true
const fn = function () {}
fn instanceof Object // true
Object.prototype.toString.call()
Object.prototype.toString.call(1) // "[object Number]"
Object.prototype.toString.call("s") // "[object Number]"
Object.prototype.toString.call(true) // "[object Boolean]"
Object.prototype.toString.call(null) // "[object Null]"
Object.prototype.toString.call(undefined) // "[object Undefined]"
Object.prototype.toString.call([]) // "[object Array]"
Object.prototype.toString.call([]) // "[object Array]"
Object.prototype.toString.call(() => {}) // "[object Function]"
Object.prototype.toString.call(new Date) // "[object Date]"
Object.prototype.toString.call(Symbol('S')) // "[object Symbol]"
NaN
typeof NaN // number
const a = NaN
a== a // false
a=== a // false
isNaN(a) // true
