Frontend interview practice question

Why does typeof null return 'object'? Data types, typeof, and NaN checks

HighIntermediateJavascript

Interview quick answer

Explain the eight JavaScript data types through the runtime checks that go wrong: typeof null is 'object', arrays are objects, NaN is a number that is not equal to itself, and global isNaN coerces its input. Shows when typeof is enough and which check to pair it with.

Interview focus

This JavaScript interview question tests whether you can explain typeof null Is 'object': Data Types and NaN Checks, connect it to production trade-offs, and handle common follow-up questions.

  • typeof null Is 'object': Data Types and NaN Checks explanation without falling back to memorized definitions
  • Typeof and Data Types reasoning, edge cases, and production failure modes
  • How you would answer the most likely JavaScript interview follow-up
Practice more JavaScript interview questions
Interview answer drill

Use this JavaScript interview question to rehearse a quick answer, common mistake, follow-up, and production pitfall.

Full interview answer

The quirk to explain

typeof null returns 'object' because of a bug in the first JavaScript engine: values carried a type tag, objects used tag 0, and null was the null pointer, also all zeros. Fixing it would break the web, so it stays. Interviewers use it to check whether you know the eight data types and which runtime check to reach for when typeof lies: value === null for null, Array.isArray for arrays, and Number.isNaN for the one value that is not equal to itself.

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

  1. Treating typeof value === 'object' as enough to detect plain objects.
  2. Forgetting arrays also return 'object'.
  3. Forgetting null also returns 'object'.
  4. 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.

The eight data types

Primitive values are stored directly, immutable, and compared by value. Objects are mutable and compared by reference. Everything that is not one of the seven primitives, including arrays, functions, dates, and regular expressions, is an object.

Category

Data type

Description

Example

Primitive

Number

Integers and floating-point numbers, plus NaN and Infinity.

42, 3.14, NaN

Primitive

String

Text data in single, double, or backtick quotes.

'Hello', `Hi ${name}`

Primitive

Boolean

true or false.

true

Primitive

Undefined

Declared but never assigned.

let x; // undefined

Primitive

Null

Intentional empty value.

let y = null;

Primitive

Symbol

Unique, immutable value, often an object key.

Symbol('id')

Primitive

BigInt

Integers beyond 2^53 - 1.

12345678901234567890n

Non-primitive

Object

Collections and complex entities, including arrays and functions.

{ name: 'Alice' }, [1, 2]

JavaScript data types at a glance.
JAVASCRIPT
let str = 'Hi';
str[0] = 'Y'; // no effect: primitives are immutable
console.log(str); // 'Hi'

const user = { name: 'Alice' };
const ref = user;
ref.name = 'Bob';
console.log(user.name); // 'Bob' (same reference)
                  

NaN: the number that is not equal to itself

NaN is a production validation pitfall because NaN !== NaN, global isNaN coerces values, and empty strings or bad payloads can silently turn checks into bugs. The safer debugging rule is Number.isNaN.

JAVASCRIPT
console.log(0 / 0);          // NaN
console.log(parseInt('Hi'));  // NaN
console.log(typeof NaN);      // 'number'
console.log(NaN === NaN);     // false

isNaN('Hello');        // true (string coerced first)
Number.isNaN('Hello'); // false
Number.isNaN(NaN);     // true
                  

Property

Description

Example

Type

The type of NaN is actually number.

typeof NaN // 'number'

Equality

NaN is the only value in JavaScript that is not equal to itself.

NaN === NaN // false

Check

Use Number.isNaN() to check if a value is truly NaN.

Number.isNaN(NaN) // true

Key characteristics of NaN in JavaScript.

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.

Guides
Preparing for interviews?

Built in public

The code behind FrontendAtlas lives on GitHub. A star helps other frontend engineers find it.

Star on GitHub