Skip to content

JavaScript typeof() operator | Find the data type

  • by

JavaScript typeof() operator is used to find the data type of a JavaScript variable. This operator returns the type of variables and values.

typeof operand

typeof(operand)

The typeof operator is not a variable. It is an operator. Operators ( + – * / ) do not have any data type. But, the typeof operator always returns a string (containing the type of the operand).

Here’s the typeof operator explained in a tabular format:

OperandReturn Value
undefined"undefined"
null"object"
true or false"boolean"
Any number"number"
Any string"string"
Symbol()"symbol"
function(){}"function"
{} (empty object)"object"
Any other object"object"

Note: the typeof null returning "object" is considered a quirk in JavaScript’s history and not an accurate reflection of the value’s type.

JavaScript typeof operator

Simple example code.

<!DOCTYPE html>
<html>
<body>

  <script>

   const num = 100;

   console.log(typeof 100); 
   console.log(typeof '0'); 
   console.log(typeof false); 
 </script>

</body>
</html> 

Output:

JavaScript typeof() operator
typeof undefined;  // "undefined"
typeof true;       // "boolean"
typeof 42;         // "number"
typeof "Hello";    // "string"
typeof Symbol();   // "symbol"
typeof function(){}; // "function"
typeof {};         // "object"
typeof null;       // "object" (quirk)

The possible types that are available in JavaScript that typeof operator returns are:

Typestypeof Result
String“string”
Number“number”
BigInt“bigint”
Boolean“boolean”
Object“object”
Symbol“symbol”
undefined“undefined”
null“object”
function“function”

More Example code

typeof "John"                 // Returns "string"
typeof 3.14                   // Returns "number"
typeof NaN                    // Returns "number"
typeof false                  // Returns "boolean"
typeof [1,2,3,4]              // Returns "object"
typeof {name:'John', age:34}  // Returns "object"
typeof new Date()             // Returns "object"
typeof function () {}         // Returns "function"
typeof myCar                  // Returns "undefined" *
typeof null                   // Returns "object" 

Do comment if you have any doubts or suggestions on this JS operator topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *