The Typeof Operator

You can use the JavaScript typeof operator to find the type of a JavaScript variable:

  typeof "John"  <=== string data type
  typeof 3.14  <=== number data type
  typeof false  <=== boolean data type
  typeof [1,2,3,4]  <=== object data type
  typeof {name:'John', age:34}  <=== object data type

HTML file: Displayed by browser:
<body>
<p>The typeof operator returns the type of a variable or an expression.</p>
<p id="demo"></p>
<script>
   document.getElementById("demo").innerHTML =
   typeof "john" + "<br />" +
   typeof 3.14 + "<br />" +
   typeof false + "<br />" +
   typeof [1,2,3,4] + "<br />" +
   typeof {name:'john', age:34};
</script>
</body>

The typeof operator returns the type of a variable or an expression.

In JavaScript, an array is a special type of object. Therefore typeof [1,2,3,4] returns object.

BackTable of ContentsNext