What is typeof Operator in JavaScript?

JavascriptWeb DevelopmentFront End Technology

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.

Here is a list of the return values for the typeof Operator.

Type
String Returned by typeof
Number
"number"
String
"string"
Boolean
"boolean"
object
"object"
Function
"function"
Undefined
"undefined"
Null
"object"

Example

The following code shows how to implement typeof operator −

<html>
   <body>
      <script>
         var a = 10;
         var b = "String";
         var linebreak = "<br />";

         result = (typeof b == "string" ? "B is String" : "Bis Numeric");
         document.write("Result => ");
         document.write(result);
         document.write(linebreak);

         result = (typeof a == "string" ? "A is String" : "Ais Numeric");
         document.write("Result => ");
         document.write(result);
         document.write(linebreak);
      </script>
   </body>
</html>
raja
Published on 19-Jan-2018 05:35:54
Advertisements