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.
How to check data type in JavaScript using typeof
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.
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.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.
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 the relevant interview-question hub first, then move into a concrete study plan before targeted company sets.