The typeof operator returns the type of variables and values. For example,
const a = 9;
console.log(typeof a); // number
console.log(typeof '9'); // string
console.log(typeof false); // boolean
Syntax of typeof Operator
The syntax of typeof operator is:
typeof operand
Here, operand is a variable name or a value.
typeof Types
The possible types that are available in JavaScript that typeof operator returns are:
| Types | typeof Result |
|---|---|
String |
"string" |
Number |
"number" |
BigInt |
"bigint" |
Boolean |
"boolean" |
Object |
"object" |
Symbol |
"symbol" |
undefined |
"undefined" |
null |
"object" |
| function | "function" |
Example 1: typeof for String
Example 2: typeof for Number
Example 3: typeof for BigInt
Example 4: typeof for Boolean
Example 5: typeof for Undefined
Example 6: typeof for null
Example 7: typeof for Symbol
Example 8: typeof for Object
Example 9: typeof for Function
Uses of typeof Operator
- The
typeofoperator can be used to check the type of a variable at a particular point. For example,
let count = 4;
console.log(typeof count);
count = true;
console.log(typeof count);
- You can perform different actions for different types of data. For example,
let count = 4;
if(typeof count === 'number') {
// perform some action
}
else if (typeof count = 'boolean') {
// perform another action
}