How to Check Data Type in JavaScript
What you’ll build or solve
You’ll correctly detect primitive values, arrays, class instances, and edge cases.
When this approach works best
Use this approach when you:
Learn JavaScript on Mimo
- Validate function inputs before processing them.
- Debug unexpected values like
undefinedornull. - Write reusable utilities that must handle multiple data types.
Skip heavy runtime checks when:
- You fully control the input source.
- You are using TypeScript and rely on static types.
Prerequisites
- A
.jsfile or browser console - Basic understanding of variables and objects
No additional setup is required.
Step-by-step instructions
1) Use typeof for primitive values
typeof is best for checking primitive types.
constname="Alex";
constage=28;
constisActive=true;
constvalue=undefined;
console.log(typeofname);// "string"
console.log(typeofage);// "number"
console.log(typeofisActive);// "boolean"
console.log(typeofvalue);// "undefined"
It also works for functions:
functiongreet() {}
console.log(typeofgreet);// "function"
Use typeof when checking:
stringnumberbooleanundefinedfunction
What to look for
typeofalways returns a string.- Arrays return
"object". typeof nullalso returns"object"due to a historical quirk.
If you need to detect null, check directly:
if (value===null) {
console.log("Value is null");
}
2) Use Array.isArray() for arrays
typeof cannot reliably detect arrays because arrays are objects.
constskills= ["HTML","CSS"];
console.log(typeofskills);// "object"
console.log(Array.isArray(skills));// true
Always use Array.isArray() when checking for arrays.
This method returns:
truefor arraysfalsefor everything else
What to look for
- Do not rely on
typeoffor arrays. Array.isArray()works consistently across environments.
3) Use instanceof for objects and classes
instanceof checks whether an object was created by a specific constructor.
constdate=newDate();
console.log(dateinstanceofDate);// true
For custom classes:
classUser {}
constuser=newUser();
console.log(userinstanceofUser);// true
Use instanceof when checking:
Dateobjects- Custom classes
- Built-in object constructors
What to look for
instanceofworks only with objects.- It checks the prototype chain.
- It does not work for primitive values.
4) Use Object.prototype.toString.call() for precise detection
For edge cases or cross-environment checks, use this method.
constvalue= [];
console.log(Object.prototype.toString.call(value));
// "[object Array]"
More examples:
JavaScript
console.log(Object.prototype.toString.call(null));
// "[object Null]"
console.log(Object.prototype.toString.call(newDate()));
// "[object Date]"
This method gives consistent results even when typeof or instanceof may be misleading.
Use this when:
- You need precise detection.
- You handle unknown or dynamic input.
- You write utility libraries.
Examples you can copy
Example 1: Validate a number input
functiondouble(value) {
if (typeofvalue!=="number") {
console.error("Expected a number");
return;
}
returnvalue*2;
}
double(5);
double("5");
Example 2: Detect arrays safely
functionprintLength(data) {
if (Array.isArray(data)) {
console.log("Array length:",data.length);
}else {
console.log("Not an array");
}
}
printLength([1,2,3]);
printLength("text");
Example 3: Check class instances
classAdmin {}
functioncheckRole(user) {
if (userinstanceofAdmin) {
console.log("Admin access granted");
}
}
checkRole(newAdmin());
Common mistakes and how to fix them
Mistake 1: Using typeof for arrays
What you might do:
typeof [1,2,3];// "object"
Why it breaks: Arrays are objects, so typeof cannot distinguish them.
Fix:
Array.isArray([1,2,3]);
Mistake 2: Misunderstanding typeof null
What you might do:
typeofnull;// "object"
Why it breaks: This is a historical behavior in JavaScript.
Fix:
value===null;
Mistake 3: Using instanceof with primitives
What you might do:
"Alex"instanceofString;
Why it breaks: Primitive values are not instances of constructors.
Fix:
typeof"Alex"==="string";
Troubleshooting
If typeof returns "object" unexpectedly, check for null or arrays.
If instanceof returns false, confirm the object was created with that constructor.
If your type check fails across modules or frames, consider Object.prototype.toString.call().
If no output appears, confirm you are running the correct file.
If Node shows nothing, verify the file contains console.log statements.
Quick recap
- Use
typeoffor primitive values and functions. - Use
Array.isArray()for arrays. - Use
instanceoffor constructor and class checks. - Check
value === nullfornull. - Use
Object.prototype.toString.call()for precise detection.
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot