Introduction
The typeof operator is a built-in JavaScript operator used to determine the data type of a value or variable. It returns the type as a string, making it easy to identify whether a value is a number, string, boolean, object, function, or another JavaScript data type.
The typeof operator is one of the most commonly used debugging and validation tools in JavaScript. It is especially useful in automation testing, where data received from APIs, user input, files, or databases may need to be validated before processing.
What is the typeof Operator?
The typeof operator returns the data type of a variable, value, or expression as a string.
Syntax
typeof value
or
typeof(value)
Both syntaxes produce the same result.
Why Do We Need the typeof Operator?
The typeof operator helps us:
Identify the data type of variables.
Validate user input.
Debug JavaScript programs.
Handle different data types correctly.
Validate API responses.
Prevent runtime errors.
Checking the Type of a Number
let age = 25;
console.log(typeof age);
Output
number
Checking the Type of a String
let language = "JavaScript";
console.log(typeof language);
Output
string
Checking the Type of a Boolean
let isLoggedIn = true;
console.log(typeof isLoggedIn);
Output
boolean
Checking the Type of undefined
let value;
console.log(typeof value);
Output
undefined
Checking the Type of null
let data = null;
console.log(typeof data);
Output
object
Note: This is a well-known historical bug in JavaScript. Although
typeof nullreturns"object",nullis a primitive value.
Checking the Type of a BigInt
let bigNumber = 12345678901234567890n;
console.log(typeof bigNumber);
Output
bigint
Checking the Type of a Symbol
let id = Symbol("user");
console.log(typeof id);
Output
symbol
Checking the Type of an Object
const student = {
name: "John",
age: 22
};
console.log(typeof student);
Output
object
Checking the Type of an Array
const colors = ["Red", "Blue", "Green"];
console.log(typeof colors);
Output
object
Arrays are objects in JavaScript, so typeof returns "object".
Checking the Type of a Function
function greet() {
console.log("Welcome");
}
console.log(typeof greet);
Output
function
Functions are a special type of object, and typeof returns "function".
Checking the Type of a Date
const today = new Date();
console.log(typeof today);
Output
object
Checking the Type of NaN
console.log(typeof NaN);
Output
number
Although NaN means Not-a-Number, its type is still "number".
Using typeof with Expressions
console.log(typeof (10 + 20));
Output
number
console.log(typeof ("Hello" + " World"));
Output
string
typeof Return Values
| Value | typeof Result |
|---|---|
"Hello" | "string" |
25 | "number" |
true | "boolean" |
undefined | "undefined" |
null | "object" |
123n | "bigint" |
Symbol() | "symbol" |
{} | "object" |
[] | "object" |
function(){} | "function" |
Real-World Example
Suppose a user enters their age through a form.
let age = "25";
console.log(typeof age);
Output
string
Before performing calculations, you know that the value should first be converted into a number.
Automation Testing Example
Automation engineers often validate API responses before performing assertions.
const response = {
status: 200,
message: "Success"
};
console.log(typeof response.status);
console.log(typeof response.message);
Output
number
string
This confirms that the API returned values with the expected data types.
Another example:
const expectedPrice = 1500;
const actualPrice = "1500";
console.log(typeof expectedPrice);
console.log(typeof actualPrice);
Output
number
string
This helps identify why a comparison might fail and indicates that type conversion may be needed.
Common Mistakes
Assuming Arrays Have Their Own Type
Incorrect assumption:
const numbers = [1, 2, 3];
console.log(typeof numbers);
Output
object
Use Array.isArray() if you need to check whether a value is an array.
console.log(Array.isArray(numbers));
Output
true
Assuming null Returns "null"
Incorrect assumption:
console.log(typeof null);
Output
object
This is a historical behavior of JavaScript.
Confusing NaN
console.log(typeof NaN);
Output
number
Although NaN represents an invalid numeric value, its type is still "number".
Best Practices
Use typeof Before Processing Data
Verify the data type before performing calculations or validations.
Use Array.isArray() for Arrays
Instead of relying on typeof, use:
Array.isArray(value)
to check whether a value is an array.
Use Strict Equality Along with typeof
Example:
if (typeof age === "number") {
console.log("Valid number");
}
This ensures that only values of the expected type are processed.
Validate API Responses
Use typeof to confirm that API responses contain values of the expected data types before making assertions.
Conclusion
The typeof operator is one of the most useful tools in JavaScript for identifying the data type of variables and values. It is widely used for debugging, input validation, API testing, and writing robust JavaScript applications.
Although there are a few special cases—such as typeof null returning "object" and arrays also returning "object"—understanding these behaviors helps developers avoid common mistakes and write more reliable code.
Frequently Asked Questions (FAQs)
What is the typeof operator in JavaScript?
The typeof operator returns the data type of a value or variable as a string.
What does typeof null return?
It returns:
object
This is a historical behavior of JavaScript.
What does typeof [] return?
It returns:
object
To check whether a value is an array, use:
Array.isArray(value)
What does typeof function(){} return?
It returns:
function
Why is the typeof operator useful in automation testing?
Automation engineers use typeof to validate API responses, user input, JSON data, and other values before performing assertions or calculations.
Key Takeaways
The
typeofoperator returns the data type of a value as a string.It can identify primitive data types such as
number,string,boolean,undefined,bigint, andsymbol.typeof nullreturns"object"due to a historical JavaScript behavior.Arrays are also reported as
"object"bytypeof.Functions return
"function"when checked withtypeof.typeof NaNreturns"number".Use
Array.isArray()to check for arrays.Use
typeoffor debugging and data validation.It is widely used in automation testing to verify API responses and user input.
Understanding the
typeofoperator helps write safer and more reliable JavaScript code.
