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.

const name = "Alex";
const age = 28;
const isActive = true;
const value = undefined;

console.log(typeof name); // "string"
console.log(typeof age); // "number"
console.log(typeof isActive); // "boolean"
console.log(typeof value); // "undefined"

It also works for functions:

function greet() {}
console.log(typeof greet); // "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.

const skills = ["HTML", "CSS"];

console.log(typeof skills); // "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.

const date = new Date();

console.log(date instanceof Date); // true

For custom classes:

class User {}

const user = new User();

console.log(user instanceof User); // 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.

const value = [];

console.log(Object.prototype.toString.call(value));
// "[object Array]"

More examples:

console.log(Object.prototype.toString.call(null));
// "[object Null]"

console.log(Object.prototype.toString.call(new Date()));
// "[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

function double(value) {
  if (typeof value !== "number") {
    console.error("Expected a number");
    return;
  }

  return value * 2;
}

double(5);
double("5");

Example 2: Detect arrays safely

function printLength(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

class Admin {}

function checkRole(user) {
  if (user instanceof Admin) {
    console.log("Admin access granted");
  }
}

checkRole(new Admin());

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:

typeof null; // "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" instanceof String;

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.