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:

  • Validate function inputs before processing them.
  • Debug unexpected values like undefined or null.
  • 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 .js file 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:

  • string
  • number
  • boolean
  • undefined
  • function

What to look for

  • typeof always returns a string.
  • Arrays return "object".
  • typeof null also 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:

  • true for arrays
  • false for everything else

What to look for

  • Do not rely on typeof for 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:

  • Date objects
  • Custom classes
  • Built-in object constructors

What to look for

  • instanceof works 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 typeof for primitive values and functions.
  • Use Array.isArray() for arrays.
  • Use instanceof for constructor and class checks.
  • Check value === null for null.
  • Use Object.prototype.toString.call() for precise detection.