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.
Frontend interview practice question
Why does typeof null return 'object'? Data types, typeof, and NaN checks
Interview quick answer
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
Use this JavaScript interview question to rehearse a quick answer, common mistake, follow-up, and production pitfall.
Full interview answer
The quirk to explaintypeof 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.
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 |
|---|---|---|
|
| Historical JavaScript quirk; check with |
Array |
| Use |
|
| Use |
Function |
| Functions are callable objects with a dedicated typeof result. |
Undeclared identifier |
| Safe in many legacy checks, but see TDZ caveat below. |
Most Common Pitfalls
- Treating
typeof value === 'object'as enough to detect plain objects. - Forgetting arrays also return
'object'. - Forgetting
nullalso returns'object'. - Using global
isNaNinstead ofNumber.isNaN.
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.
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:
typeoffor primitives/functions/undefined checksvalue === nullfor nullArray.isArrayfor arraysNumber.isNaNfor NaNObject.prototype.toString.call(value)for precise built-in tags
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 |
| Fast and readable for string/number/boolean/undefined/symbol/bigint/function. |
Array detection |
| Distinguishes arrays from other objects reliably. |
NaN detection |
| No coercion, unlike global |
Fine-grained object type |
| Reliable built-in tag extraction for Date, RegExp, etc. |
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 |
|
Primitive | String | Text data in single, double, or backtick quotes. |
|
Primitive | Boolean |
|
|
Primitive | Undefined | Declared but never assigned. |
|
Primitive | Null | Intentional empty value. |
|
Primitive | Symbol | Unique, immutable value, often an object key. |
|
Primitive | BigInt | Integers beyond |
|
Non-primitive | Object | Collections and complex entities, including arrays and functions. |
|
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 itselfNaN 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.
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 |
|
Equality |
|
|
Check | Use |
|
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
NaNas a normal numeric input.
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.
Use this as one explanation rep, then continue with the JavaScript interview questions cluster or a guided prep path.
Built in public
The code behind FrontendAtlas lives on GitHub. A star helps other frontend engineers find it.