typeof(undefined) // "undefined"
typeof(null) // "object"
typeof([1,2]) // "object"
typeof({a:1}) // "object"
typeof('123') // "string"
typeof(1) // "number"
typeof(true) // "boolean"
typeof(Array) // "function"
typeof(() => {}) // typeof 箭头函数返回也是 "function"
谨记typeof
的返回值都是 string
类型
typeof(typeof(null)) // "string"
typeof(typeof(undefined)) // "string"
typeof(undefined) === undefined // false
typeof(undefined) === "undefined" // true
特别的:
null
返回的是"object"
,undefined
返回还是"undefined"
typeof 可以返回的类型为:
number
、string
、boolean
、undefined
、object
、function
判断数据是否是数组类型:
arr = []
arr instanceof Array // instanceof判断方法
Array.prototype.isPrototypeOf(arr) // 原型链判断
Array.isArray(arr) // JS 数组方法Array中的isArray方法
最全的判断方式:
Object.prototype.toString.call()