typeof
- typeof用于判断数据类型,除了null以外都能正确判断(typeof null会返回object),在引用类型中除了函数的返回类型是function,其他都是object
- 在JS最初采用32位系统,为了性能采用了低位存储了变量的类型信息,用000开头表示object,而null是全0就被识别为了object
typeof null // object
typeof 112 // number
typeof "hah" // string
typeof true // boolean
typeof {} // object
typeof [] // object
typeof function(){} // function
typeof undefined // undefined
typeof 11n // bigint
typeof Symbol() // symbol
对于undefined
let a
a === undefined // 可以用这种方法判断是否为undefined
// 但是undefined不是保留字,在低版本的浏览器中,undefined能够被赋值
let undefined = 1 // 判断就会出错
// 可以用void随便跟上一个组成表达式返回是undefined
a === void 0