JavaScript typeof Operator
JavaScript typeof operator is used to get a string representing the datatype of the value given as operand.
Syntax
The syntax to use typeof operator to get the datatype of variable x is
</>
Copy
typeof x
//or
typeof(x)
Example
In the following example, we take a numeric value in x
, and programmatically get the datatype of x
using typeof operator.
index.html
</>
Copy
<!DOCTYPE html>
<html lang="en">
<body>
<pre id="output"></pre>
<script>
var x = 25;
var result = typeof x;
document.getElementById('output').innerHTML += result;
</script>
</body>
</html>
Now, let us take a numeric value in x
, and programmatically get the datatype of x
using typeof operator.
index.html
</>
Copy
<!DOCTYPE html>
<html lang="en">
<body>
<pre id="output"></pre>
<script>
var x = 'Hello World';
var result = typeof x;
document.getElementById('output').innerHTML += result;
</script>
</body>
</html>
Conclusion
In this JavaScript Tutorial, we learned how to use typeof operator to find out the datatype of the given value.