JavaScript : typeof operator
Description
The typeof operator is used to get the data type (returns a string) of an expression.
Syntax
typeof (operand)
There are six possible values that typeof returns: object, boolean, function, number, string, and undefined. The following table summarizes possible values returned by the typeof operator.
| Type of the operand | Result |
|---|---|
| Object | "Object" |
| boolean | "boolean" |
| function | "function" |
| number | "number" |
| string | "string" |
| undefined | "undefined" |
Example -1 :
The following web document test the type of different variables.
HTML Code
<!doctype html> <head> <meta charset="utf-8"> <title>JavaScript typeof operator example.</title> <meta name="description" content="This document contains an example JavaScript typeof operator"/> </head> <body> <script src="javascript-typeof-operator-example1.js"></script> </body> </html>
JS Code
var x;
var y = true;
var string1 = "w3resource";
function formula()
{
"12+23-19";
}
var day1 = new Date();
var rollno = 12;
var newParagraph = document.createElement("p");
var newText = document.createTextNode('The typeof of x is : '+typeof(x));
newParagraph.appendChild(newText);
document.body.appendChild(newParagraph);
var newParagraph1 = document.createElement("p");
var newText1 = document.createTextNode('The typeof of y is : '+typeof(y));
newParagraph1.appendChild(newText1);
document.body.appendChild(newParagraph1);
var newParagraph2 = document.createElement("p");
var newText2 = document.createTextNode('The typeof of string1 is : '+typeof(string1));
newParagraph2.appendChild(newText2);
document.body.appendChild(newParagraph2);
var newParagraph4 = document.createElement("p");
var newText4 = document.createTextNode('The typeof of formula is : '+typeof(formula));
newParagraph4.appendChild(newText4);
document.body.appendChild(newParagraph4);
var newParagraph5 = document.createElement("p");
var newText5 = document.createTextNode('The typeof of day1 is : '+typeof(day1));
newParagraph5.appendChild(newText5);
document.body.appendChild(newParagraph5);
var newParagraph6 = document.createElement("p");
var newText6 = document.createTextNode('The typeof of rollno is : '+typeof(rollno));
newParagraph6.appendChild(newText6);
document.body.appendChild(newParagraph6);
View the example in the browser
Practice the example online
JavaScript typeof operator example.
Example -2 :
The following web document shows how the typeof operator (first syntax of typeof operator) is used of different variables.
HTML Code
<!doctype html>
<head> <meta charset="utf-8"> <title>Using JavaScript typeof operator example</title> <meta name="description" content="This document contains an example Using JavaScript typeof operator example"/> </head> <body> <script src="javascript-typeof-operator-example2.js"></script> </body> </html>
JS Code
function typecheck(val1)
{
if (typeof val1 === 'number')
{
var newParagraph = document.createElement("p");
var newText = document.createTextNode('The type of input value is number.');
newParagraph.appendChild(newText);
document.body.appendChild(newParagraph);
}
if (typeof val1 === 'string')
{
var newParagraph1 = document.createElement("p");
var newText1 = document.createTextNode('The type of input value is string.');
newParagraph1.appendChild(newText1);
document.body.appendChild(newParagraph1);
}
}
typecheck(200);
typecheck('abcd');
View the example in the browser

