typeof
用法
typeof operand
typeof(operand)
typeof null
> typeof null
"object"
为什么 typeof null 是 object
根据 Why is typeof null “object”?,这是 JavaScript 实现上的一个 Bug,如果修正这个 Bug,会导致现有代码出现更多的 Bug。
JavaScript 底层的 object 的 type 是使用 0 来表示的,而 null
在多数平台上也是使用 0 来表示,所以 null
的 type 也是 0,因此返回 object
。
null 是 object 吗
null
不是 object,它是 primitive value 。
typeof typeof null
> typeof typeof null
"string"
typeof Array
> typeof Array
"function"
typeof [1,2,3]
> typeof [1,2,3]
"object"
typeof 5
> typeof 5
"number"
typeof false
"boolean"
typeof undefined
typeof undefined
"undefined"
typeof String(“asdfasdf”)
typeof String("asdfasdf")
"string"