JavaScript: typeof

JavaScript: typeof

  • 2020-5-14

As to typeof operator, we need to keep 4 points in mind:

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