JavaScript typeof operator

The typeof operator is used to determine the data type of its operand. The typeof operator is easy to use and it can be invoked by simply typing typeof (variable). A string will be returned telling the type of the variable. As an example, typeof(variable) returns 'number' for variables that are integers, and 'string' for variables that are strings. Data that can have multiple types are best handled using the typeof operator. By using it, developers can ensure that the data being processed is of the correct type and that errors are avoided. Also, it can be used when debugging programs to identify the source of errors. In addition to understanding the behavior of certain functions, the typeof operator can also be useful. As an example, typeof(parseInt) returns 'function' and typeof(5) returns 'number'.


typeof (operand)

Type of the operand


Examples to check typeof a JavaScript variable

typeof 911                             // "number"
typeof "Vishal"                        // "string"
typeof true                            // "boolean"
typeof 434237139071347231391137011120n // "bigint"
typeof NaN                             // "number"
typeof [9,8,7,6,5]                     // "object"
typeof {name:'Vishal', city:'Mumbai'}  // "object"
typeof null                            // "object"
typeof new Date()                      // "object"
typeof function () {}                  // "function"
typeof movieName                       // "undefined"
typeof Symbol('symbol')                // "Symbol" 

A known quirk of JavaScript is that typeof null returns "object".

typeof null === "object" 

It is caused by a historical error in the languages design. As a primitive type, null is not a member of any object hierarchy. In addition, typeof returns "function" when used with functions.

const fun = new Function();
typeof fun  // "function"