类型判断(数组、对象、null、undefined、NaN)

68 阅读1分钟

wallhaven-r22qy1.jpg

js类型:基本类型和引用类型

基本类型:string、number、boolean、null、undefined、symbol、bigint

引用类型:object (包括function、array)

1.判断类型typeof

类型:number、string、boolean、null 、undefined、symbol、function、object

注意的三点:

  • typeof null ==> "object"
  • typeof [ ] ==> “object”
  • typeof Symbol() ==> "symbol" ,es6新增的语法

2.类型判断null

null === null // true

3.类型判断undefined

typeof undefined === 'undefined' // true

undefined === undefined // true

4.类型判断null或undefined,除去(0和false)

null == null // true

undefined == null // true

undefined 是从 null 中派生出来的,简单理解就是undefined 是没有定义的,null 是定义了但是为空

5.类型判断NaN

NaN == NaN // false

isNaN(NaN) // true

6.类型判断对象

满足三种条件: (typeof xxx === 'object' )、(xxx !== null) 、( !Array.isArray(xxx) )

7.类型判断数组

[1,2] instanceof Array // true

Array.isArray([1,2]) // true

[1,2].construstor === Array // true