JavaScript typeof Operator

The typeof operator is a unary operator that is placed before its single operand, which can be of any type. Its value is a string indicating the data type of the operand. The typeof operator evaluates to “number”, “string”, or “boolean” if its operand is a number, string, or boolean value and returns true or false based on the evaluation.

The below list shows the Type and String Returned by typeof Operator.

  1. Number – “number”
  2. String – “string”
  3. Boolean – “boolean”
  4. Object – “object”
  5. Function – “function”
  6. Undefined – “undefined”
  7. Null – “object”

Example

<html>
   <body>
   
      <h1>JavaScript typeof Operator</h1>
    
      <script type="text/javascript">
            var a = 10;
            var b = "Welcome to easy code";
            var linebreak = "<br />";
         
            result = (typeof b == "string" ? "B is String" : "B is Numeric");
            document.write("Result => ");
            document.write(result);
            document.write(linebreak);
            document.write(linebreak);
         
            result = (typeof a == "string" ? "A is String" : "A is Numeric");
            document.write("Result => ");
            document.write(result);
            document.write(linebreak);
      </script>     
   </body>
   
  <br>
  <p>Powered by easycode.onewebapp.com</p>
</html>

 

Output

JavaScript typeof Operator