JavaScript: typeof
- 2020-5-14
As to typeof
operator, we need to keep 4 points in mind:
- It returns a string ('undefined', 'object', 'boolean', 'number', 'bigint', 'string', 'symbol' and 'function')
typeof null === 'object'
(a never fixed bug, see here. sincenull
is a falsy value, so we can use the following expression to test it:typeof value === 'object' && !value
. A more simple way isvalue === null
since null type only contains one value - null);typeof NaN === 'number'
(even if NaN is "not a number", Use Number.isNaN to test NaN instead)typeof [] === 'object'
(Use Array.isArray to test array instead)