JavaScript Tutorial JavaScript Resources
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

JavaScript - typeof operator



The JavaScript typeof operator returns the type of a JavaScript variable or expression. The syntax for using this operator is given below:

Syntax

typeof operand
typeof(operand)

Parameters

operand Specify an expression or a variable whose type is to be returned.

Example:

In the example below, the typeof operator is used to find the datatype of a given variable or expression.

var txt;

//Numbers
txt = "typeof 25 : " + typeof 25 + "<br>";
txt = txt + "typeof 25.5 : " + typeof 25.5 + "<br>";
txt = txt + "typeof(10.5) : " + typeof(10.5) + "<br>";
txt = txt + "typeof(10 + 20) : " + typeof(10 + 20) + "<br><br>";

//Strings
txt = txt + "typeof '' : " + typeof '' + "<br>";
txt = txt + "typeof 'John' : " + typeof 'John' + "<br>";
txt = txt + "typeof 'John D' : " + typeof 'John D' + "<br><br>";

//Booleans
txt = txt + "typeof true : " + typeof true + "<br>";
txt = txt + "typeof false : " + typeof false + "<br>";
txt = txt + "typeof Boolean(1) : " + typeof Boolean(1) + "<br><br>";

//Symbols
txt = txt + "typeof Symbol() : " + typeof Symbol() + "<br>";
txt = txt + "typeof Symbol('foo') : " + typeof Symbol('foo') + "<br><br>";

//Undefined
txt = txt + "typeof undefined : " + typeof undefined + "<br><br>";

//Objects
txt = txt + "typeof {a: 1} : " + typeof {a: 1} + "<br>";
txt = txt + "typeof [1, 2, 3] : " + typeof [1, 2, 3] + "<br><br>";

//Functions
txt = txt + "typeof function() {} : " + typeof function() {} + "<br><br>";

The output (value of txt) after running above script will be:

typeof 25 : number
typeof 25.5 : number
typeof(10.5) : number
typeof(10 + 20) : number

typeof '' : string
typeof 'John' : string
typeof 'John D' : string

typeof true : boolean
typeof false : boolean
typeof Boolean(1) : boolean

typeof Symbol() : symbol
typeof Symbol('foo') : symbol

typeof undefined : undefined

typeof {a: 1} : object
typeof [1, 2, 3] : object

typeof function() {} : function

The following table summarizes the possible return values of typeof operator.

TypeResult
Undefined"undefined"
Null"object"
Boolean"boolean"
Number"number"
BigInt"bigint"
String"string"
Symbol"symbol"
Function object"function"
Any other object"object"

❮ JavaScript - Operators

5