JavaScript is a dynamic programming language. Because of this, a variable can store different types of values such as numbers, strings, or objects in JavaScript. However, sometimes developers need to check the type of a variable before using it.
This is where the JavaScript typeof operator becomes useful. It helps you quickly identify the data type of a variable or value. As a result, you can write safer and more reliable code.
In this guide, you will learn the syntax, examples, and return values of the JavaScript typeof operator in a simple and easy way.
What Is the JavaScript typeof Operator?
The JavaScript typeof operator is used to determine the data type of a value or variable. It returns the type as a string, such as “number”, “string”, or “boolean”.
Developers often use this operator when they want to validate input or check data before performing operations.
For example, imagine a calculator app. The app should only accept numbers. Therefore, you can use the typeof operator to check the input type before performing calculations.
Example
let value = 10;
console.log(typeof value);
Output
number
In this example, JavaScript checks the type of the variable value. Since the variable contains a number, the result is “number”.
Syntax of JavaScript typeof Operator
The syntax of the JavaScript typeof operator is very simple.
typeof operand
Or you can also write it like this:
typeof(operand)
Here:
- operand represents the value or variable whose type you want to check.
Both syntax styles work the same way in JavaScript.
Example
console.log(typeof "Hello");
console.log(typeof 25);
console.log(typeof true);
Output
string
number
boolean
In this example, the typeof operator checks different values and returns their types.
JavaScript typeof Return Values
The JavaScript typeof operator can return several different values depending on the type of data.
Below is a table that explains the possible return values.
| Data Type | Return Value |
| Number | “number” |
| String | “string” |
| Boolean | “boolean” |
| Undefined | “undefined” |
| Object | “object” |
| Function | “function” |
| BigInt | “bigint” |
| Symbol | “symbol” |
JavaScript always returns these results as strings.
For example, if you check a string value, the operator returns “string” instead of the actual text.
JavaScript typeof Examples
Now let’s understand the JavaScript typeof operator with some practical examples.
1- Checking a Number
First, let’s check a numeric value.
let price = 100;
console.log(typeof price);
Output
number
For example, an e-commerce website may check the price value before applying discounts.
2- Checking a String
Next, let’s check a text value.
let name = "John";
console.log(typeof name);
Output
string
For example, a login system may check whether a username is a string before processing it.
3- Checking a Boolean
Now let’s check a Boolean value.
let isLoggedIn = true;
console.log(typeof isLoggedIn);
Output
boolean
For example, a website may use a boolean variable to track whether a user is logged in.
4- Checking Undefined Variables
Sometimes a variable exists but does not contain any value. In this case, JavaScript treats it as undefined.
let data;
console.log(typeof data);
Output
undefined
For example, if a user has not filled out a form field yet, the value may be undefined.
5- Checking Objects
JavaScript objects store collections of data.
let user = {name: "Alice", age: 25};
console.log(typeof user);
Output
object
For example, many applications store user information in objects.
6- Checking Functions
Functions in JavaScript are also special objects.
function greet() {
return "Hello";
}
console.log(typeof greet);
Output
function
For example, JavaScript frameworks often check whether a variable is a function before calling it.
Special Cases of JavaScript typeof
Although the JavaScript typeof operator is useful, it has some special cases that developers should understand.
1- typeof null
One interesting behavior in JavaScript is:
console.log(typeof null);
Output
object
This happens because of a historical issue in JavaScript. Even though null represents the absence of a value, the operator still returns “object”.
2- typeof NaN
Another surprising case is NaN in JavaScript:
console.log(typeof NaN);
Output
number
Even though NaN means Not a Number, JavaScript still treats it as a numeric value.
3- typeof Arrays
Arrays are actually objects in JavaScript.
let fruits = ["Apple", "Banana", "Orange"];
console.log(typeof fruits);
Output
object
Therefore, developers usually use Array.isArray() instead of typeof when checking arrays.
Why Developers Use the typeof Operator
Developers use the JavaScript typeof operator in many situations.
First, it helps prevent errors in dynamic code. Second, it allows programs to behave differently depending on the data type.
Example
function processData(value) {
if (typeof value === "number") {
console.log("Processing number");
} else {
console.log("Invalid data type");
}
}
processData(50);
processData("Hello");
In this example, the function checks the type before performing any operation. As a result, it avoids unexpected errors.
Advantages of JavaScript typeof
The JavaScript typeof operator provides several benefits.
1- Easy to Use
First, the syntax is very simple. Developers can quickly check the type of any value.
2- Works Without Dependencies
You do not need to import any library to use it.
3- Useful for Debugging
Developers often use typeof to debug code and understand what type of data a variable contains.
Limitations of the typeof Operator
Even though the JavaScript typeof operator is helpful, it has some limitations.
1- Cannot Identify Arrays
For example:
typeof []
returns “object” instead of “array”.
2- typeof null Issue
Another limitation is that typeof null returns “object”.
3- Cannot Detect Specific Object Types
For example, it cannot distinguish between different object classes.
Therefore, developers sometimes use other methods like:
- instanceof
- Array.isArray()
- Object.prototype.toString
Bottom Line
The JavaScript typeof operator is a simple yet powerful tool for checking data types in JavaScript. It helps developers understand what type of value a variable contains and prevents errors in dynamic programs.
Moreover, the syntax is very easy to use, and it works without any additional libraries. However, developers should also understand its limitations, such as the typeof null behavior and array detection issues.
Overall, if you want to write safer JavaScript code, learning how to use the typeof operator is essential.
FAQs
What is the typeof operator in JavaScript?
The JavaScript typeof operator checks the type of a variable or value and returns it as a string.
What does typeof return?
It returns values like “string”, “number”, “boolean”, “object”, “function”, “undefined”, “symbol”, or “bigint” depending on the data type.
Why does typeof null return object?
This behavior exists because of a historical bug in JavaScript’s early implementation, and it still remains for compatibility reasons.
Can typeof detect arrays?
No. The typeof operator returns “object” for arrays. Therefore, developers usually use Array.isArray() to check arrays.
Read Also – Operators in JavaScript

