typeof Operator

The typeof operator tells the data type of a given variable. The typeof operator returns one of the following strings:

Returned ValueMeanings
"undefined"if the value is undefined
"boolean"if the value is a Boolean
"string"if the value is a string
"number"if the value is a number
"object"if the value is an object or null
"function"if the value is a function
 
<!DOCTYPE html>
<html>
<head>
    <title>typeof Example</title>
    <script type="text/javascript">

    var aString = "string";
    //The typeof is an operator and not a function, no parentheses are required. 
    document.writeln(typeof aString);    //"string"
    document.writeln(typeof(aString));   //"string" 
    document.writeln(typeof 10);         //"number"
      
    </script>
</head>
<body>
</body>
</html>
  
Click to view the demo

Calling typeof null returns a value of "object"

 
<!DOCTYPE html>
<html>
<head>
    <title>typeof Example</title>
    <script type="text/javascript">

        var aString = null;
        document.writeln(typeof aString);    //"object"
      
    </script>
</head>
<body>
</body>
</html>
  
Click to view the demo

The following code calls typeof on an undefined variable.

 
<!DOCTYPE html>
<html>
<head>
    <title>typeof Example</title>
    <script type="text/javascript">

        var aString;
        document.writeln(typeof aString);    //"undefined"
      
    </script>
</head>
<body>
</body>
</html>
  
Click to view the demo
Home 
  JavaScript Book 
    Language Basics  

Data Types:
  1. JavaScript Data Types
  2. typeof Operator
  3. The Undefined Type
  4. null Type
  5. null vs undefined
  6. Boolean Type
  7. Boolean() casting function
  8. The Literials of Number Type
  9. Octal Integer
  10. Hexadecimal
  11. Floating-Point Values
  12. Value range
  13. NaN
  14. Number Conversions:Number(), parseInt() and parseFloat()
  15. Number() function
  16. parseInt()
  17. parseFloat()
  18. The String Type
  19. String Literals and Escapes
  20. Get the String Length
  21. Converting to a String with toString() method
  22. Convert Number to String with radix
  23. Convert to String with String() casting function