What is the typeof operator in Javascript?

typeof Operator

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

Returned Value Meanings

"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

 

The following code uses typeof operator to get the data type information for a variable.

 







    

 

    var aString = "string";

    

    document.writeln(typeof aString);    //"string"

    document.writeln(typeof(aString));   //"string" 

    document.writeln(typeof 10);         //"number"

      

    









 

The typeof is an operator and not a function, no parentheses are required.

The typeof operator tells if a variable is a primitive type. It tells if a variable is a string, number, Boolean, or undefined. If the value is an object or null, then typeof returns “object”: