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).

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

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 *

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