Typeof JavaScript

A typeof keyword returns a string indicating the Data Type of the identifier. We use it for getting the type of primitive values.

Using Typeof

The syntax of the Typeof is as shown below. It returns the data type of the identifier, which we place right next to it.

For Example

Typeof correctly returns the type in case of number, string, boolean, symbol, undefined, function. Everything else is object. That also includes null.

The following table summarizes the possible return values of typeof.

TypeResult
Undefined“undefined”
Null“object”
Boolean“boolean”
Number“number”
BigInt“bigint”
String“string”
Symbol“symbol”
Function object“function”
Everything Else“object”

Typeof null is object

Although the null is a data type in JavaScript, typeof null returns “object”

In the first implementation of JavaScript, JavaScript values were represented as a type tag and a value. The type tag for objects was 0. null was represented as the NULL pointer (0x00 in most platforms). Consequently, null had 0 as type tag, hence the typeof return value “object”

A fix was proposed for ECMAScript (via an opt-in), but was rejected. It would have resulted in typeof null === ‘null’.

References

  1. Reference
  2. The history of “typeof null”
  3. Typeof Null Proposal

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top