javascript typeof

JavaScript typeof Operator Tutorial

JavaScript typeof operator is used to get the datatype of its operand. Operand can be any function, object or variable.

Syntax : 

typeof (operand)

JavaScript typeof Operator String Example

<html>
<head>
<title>Developer Helps | Javascript typeof Operator</title>
</head>
<body>
<h2>Javascript typeof</h2>
<p id="empty-string"></p>
<p id="single-word"></p>
<p id="multiple-word"></p>

<script>
var language1 = "";
var language2 = "PHP";
var language3 = "Java WordPress";

document.getElementById("empty-string").innerHTML = typeof(language1);
document.getElementById("single-word").innerHTML = typeof(language2);
document.getElementById("multiple-word").innerHTML = typeof(language3);
</script>

</body>
</html>

Output :

Javascript typeof

string
string
string

JavaScript Number Datatype Example

<html>
<head>
<title>Developer Helps | Javascript typeof Operator</title>
</head>
<body>
<h2>Javascript typeof</h2>
<p id="number-type"></p>

<script>
document.getElementById("number-type").innerHTML = typeof(9) + "<br>" + typeof(9.45) + "<br>" + typeof(0);
</script>

</body>
</html>

Output :

Javascript typeof
number
number
number

JavaScript Undefined Datatype Example

In JavaScript, a variable without a value, has the value undefined.

<html>
<head>
<title>Developer Helps | Javascript typeof Operator</title>
</head>
<body>
<h2>Javascript typeof</h2>
<p id="undefined-type"></p>

<script>
var lang;
document.getElementById("undefined-type").innerHTML = typeof(lang);
</script>

</body>
</html>

Output :

Javascript typeof

undefined

JavaScript Object Datatype Example

In JavaScript, typeof operator returns the “object” for the objects and arrays or null.

<html>
<head>
<title>Developer Helps | Javascript typeof Operator</title>
</head>
<body>
<h2>Javascript typeof</h2>
<p id="object-type"></p>

<script>
var arr = ["PHP", "Java"];
var obj = {language:"PHP", framework:"Laravel"};
var value = null;

document.getElementById("object-type").innerHTML = "Array is: " + typeof(arr) + "<br>Object is: " + typeof(obj) + "<br>null is: " + typeof(value);
</script>

</body>
</html>

Output :

Javascript typeof

Array is: object
Object is: object
null is: object

JavaScript Boolean Datatype Example

Boolean type value is true or false.

<html>
<head>
<title>Developer Helps | Javascript typeof Operator</title>
</head>
<body>
<h2>Javascript typeof</h2>
<p id="boolean-type"></p>

<script>
var bool = true ;
document.getElementById("boolean-type").innerHTML = typeof(bool);
</script>

</body>
</html>

Output :

Javascript typeof

boolean

Thank

Thanks for the reading post. I hope you like and understand the post. If you have any doubt regarding this post please comment below.

More Related Post

Thank you for reading our article about “JavaScript type of Operator“. If you like our article, don’t forget to share it with your programmer friends.

Leave a comment

Your email address will not be published. Required fields are marked *

0 0 0
FOLLOW ON