基本类型:number, string, boolean, undefined, null, symbol
复合类型:对象

判断类型的几种方式

typeof

  1. typeof 1 // number
  2. typeof 's' // string
  3. typeof true // boolean
  4. typeof undefined // undefined
  5. typeof null // object // 重点记: 这是一个 js 的 bug *
  6. typeof NaN // number *
  7. const v1 = {}
  8. const v2 = []
  9. const v3 = function () {}
  10. const v4 = () => {}
  11. const v5 = Symbol('s')
  12. const v6 = new Boolean(true)
  13. const v7 = Number('w')
  14. typeof v1 // object
  15. typeof v2 // object
  16. typeof v3 // function *
  17. typeof v4 // function *
  18. typeof v5 // symbol
  19. typeof v6 // object *
  20. typeof v7 // number *
  1. typeof a // ?
  2. typeof b // ?
  3. var a = 1
  4. let 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