How to check data type in JavaScript using typeof

HighIntermediateJavascript
Quick Answer

The typeof operator is the fastest built-in way to inspect value types at runtime in JavaScript, but it has well-known edge cases such as typeof null, arrays returning object, and NaN returning number. High-quality answers show when typeof is enough and when to combine it with Array.isArray, Number.isNaN, and Object.prototype.toString.

Answer

The Core Idea

Use typeof when you need a quick runtime type check in JavaScript. It returns a string like 'string', 'number', or 'object'.

It is very useful, but not complete. Senior-level usage means knowing its blind spots and combining it with a few targeted checks.

JAVASCRIPT
console.log(typeof 42);           // 'number'
console.log(typeof 'hello');      // 'string'
console.log(typeof true);         // 'boolean'
console.log(typeof undefined);    // 'undefined'
console.log(typeof 10n);          // 'bigint'
console.log(typeof Symbol('id')); // 'symbol'
console.log(typeof function () {}); // 'function'
                  

Value

typeof result

What to know

null

'object'

Historical JavaScript quirk; check with value === null.

Array

'object'

Use Array.isArray(value) for arrays.

NaN

'number'

Use Number.isNaN(value) when validating numbers.

Function

'function'

Functions are callable objects with a dedicated typeof result.

Undeclared identifier

'undefined'

Safe in many legacy checks, but see TDZ caveat below.

typeof is fast and useful, but context matters.

Most Common Pitfalls

    • Treating typeof value === 'object' as enough to detect plain objects.
    • Forgetting arrays also return 'object'.
    • Forgetting null also returns 'object'.
    • Using global isNaN instead of Number.isNaN.
JAVASCRIPT
console.log(typeof null); // 'object' (quirk)
console.log(typeof []);   // 'object'
console.log(typeof NaN);  // 'number'

console.log(Array.isArray([]));   // true
console.log(Number.isNaN(NaN));   // true
                  

Undeclared vs TDZ nuance

typeof notDeclared is usually safe and returns 'undefined'.

But variables declared with let/const are in TDZ before declaration. Accessing them (even with typeof) inside TDZ throws ReferenceError.

JAVASCRIPT
console.log(typeof maybeMissing); // 'undefined' (if identifier was never declared)

{
  // console.log(typeof inTdz); // ReferenceError
  let inTdz = 1;
  console.log(typeof inTdz); // 'number'
}
                  

Practical detection pattern

In production code, use a layered strategy:

  • typeof for primitives/functions/undefined checks
  • value === null for null
  • Array.isArray for arrays
  • Number.isNaN for NaN
  • Object.prototype.toString.call(value) for precise built-in tags
JAVASCRIPT
function getType(value) {
  if (value === null) return 'null';
  if (Array.isArray(value)) return 'array';
  if (Number.isNaN(value)) return 'nan';

  const t = typeof value;
  if (t !== 'object') return t;

  return Object.prototype.toString.call(value).slice(8, -1).toLowerCase();
}

console.log(getType([]));              // 'array'
console.log(getType(new Date()));      // 'date'
console.log(getType(/abc/));           // 'regexp'
console.log(getType(null));            // 'null'
                  

Goal

Best check

Why

Quick primitive guard

typeof

Fast and readable for string/number/boolean/undefined/symbol/bigint/function.

Array detection

Array.isArray

Distinguishes arrays from other objects reliably.

NaN detection

Number.isNaN

No coercion, unlike global isNaN.

Fine-grained object type

Object.prototype.toString.call

Reliable built-in tag extraction for Date, RegExp, etc.

Use the simplest check that is correct for the question you are asking.

Interview-ready one-liner

typeof is the primary runtime type operator in JavaScript, but robust type checks pair it with Array.isArray, value === null, and Number.isNaN to handle edge cases correctly.

Practical scenario
You receive mixed analytics events from multiple clients and must normalize payload fields before storing them. A generic typeof check alone misclassifies arrays and null values, causing schema drift and dashboard bugs.

Common pitfalls

      • Using typeof value === 'object' as a final validator.
      • Forgetting TDZ behavior when moving declarations.
      • Treating NaN as a normal numeric input.
Trade-off or test tip
Create tests with null, arrays, NaN, dates, undeclared fields, and malformed JSON values to verify your detection utility under real payload variance.

Still so complicated?

Think of typeof like a quick airport screening. It catches most cases fast. For suspicious baggage (null, arrays, NaN), you send it to a more specific scanner.

Summary

Use typeof as your first pass, not your only pass. It covers most primitive checks well, but production-grade validation needs explicit handling for null, arrays, NaN, and structured objects.

Similar questions
Guides
Preparing for interviews?

Use the relevant interview-question hub first, then move into a concrete study plan before targeted company sets.