Checking Types
Because JavaScript is dynamically typed, a function can receive any kind of value: a number, a string that looks like a number, null, an array, a date. Type checking is how your code finds out what it actually has before using it. There is no single operator that answers every question, so this lesson gives you the right tool for each case: typeof for primitives and functions, Array.isArray for arrays, instanceof for class instances, and the Number.is... family for number edge cases.
In One Minute
typeof xworks for primitives and functions. Quirks:typeof nullis"object", and arrays are"object".Array.isArray(x)checks for arrays;x === nullchecks for null.x instanceof Classchecks whether an object was made by a class or constructor (by walking its prototype chain).Number.isNaN,Number.isFinite,Number.isIntegercheck numbers without converting them.Object.prototype.toString.call(x)gives a precise tag like"[object Date]"for any value.
typeof
| Value | typeof |
|---|---|
"text" | "string" |
42, NaN, Infinity | "number" |
42n | "bigint" |
true | "boolean" |
undefined | "undefined" |
Symbol() | "symbol" |
null | "object" (historical bug) |
{}, [], new Date(), /re/ | "object" |
function () {}, class {}, () => {} | "function" |
console.log(typeof NaN);
console.log(typeof null);
console.log(typeof []);
console.log(typeof class Account {});
console.log(typeof undeclaredVariable);number
object
object
function
undefinedExplanation: NaN is a number value. null and arrays are the two famous "object" surprises. Classes are functions. And typeof on a name that was never declared returns "undefined" instead of throwing, which makes it the safe way to test whether a global feature exists: if (typeof window !== "undefined") tells you whether you are in a browser.
Checking for null and Arrays
function describe(value) {
if (value === null) return "null";
if (Array.isArray(value)) return `array of ${value.length}`;
if (typeof value === "object") return "object";
return typeof value;
}
console.log(describe(null));
console.log(describe([120, 85]));
console.log(describe({ name: "Navika" }));
console.log(describe(42));null
array of 2
object
numberCheck null and arrays before testing for "object", as this function does.
instanceof
value instanceof Constructor returns true if Constructor.prototype appears anywhere in the value's prototype chain. In practice: "was this object created by this class, or a class that extends it?"
class Payment {}
class CardPayment extends Payment {}
const p = new CardPayment();
console.log(p instanceof CardPayment);
console.log(p instanceof Payment);
console.log(p instanceof Object);
console.log(new Date() instanceof Date);
console.log([] instanceof Array);
console.log("text" instanceof String);true
true
true
true
true
falseExplanation: a CardPayment is also a Payment (because of extends) and an Object. instanceof returns false for primitives: "text" isn't an object, so it isn't an instance of anything.
Checking Numbers
The Number.is... methods check without converting their argument. The older global functions isNaN and isFinite convert first, which gives misleading answers:
console.log(isNaN("hello"), Number.isNaN("hello"));
console.log(isFinite("42"), Number.isFinite("42"));
console.log(Number.isInteger(5), Number.isInteger(5.5), Number.isInteger("5"));
console.log(Number.isSafeInteger(2 ** 53), Number.isSafeInteger(2 ** 53 - 1));true false
true false
true false false
false true| Question | Use |
|---|---|
Is it the value NaN? | Number.isNaN(x) |
| Is it a real, usable number? | Number.isFinite(x) |
| Is it a whole number? | Number.isInteger(x) |
| Is it an exact integer? | Number.isSafeInteger(x) |
typeof x === "number" alone is not enough to accept a value as a number, because NaN and Infinity pass it. Number.isFinite(x) is the best single check.
Object.prototype.toString
For a precise answer about any value, built-in objects carry an internal tag that Object.prototype.toString reveals:
const tag = (value) => Object.prototype.toString.call(value);
console.log(tag(null));
console.log(tag([]));
console.log(tag(new Date()));
console.log(tag(/fee/));
console.log(tag(new Map()));
console.log(tag(42));
console.log(tag(Promise.resolve()));[object Null]
[object Array]
[object Date]
[object RegExp]
[object Map]
[object Number]
[object Promise]Libraries use this for robust type detection. Classes can customize their tag with Symbol.toStringTag (see the symbols lesson), so treat it as a strong hint rather than an unforgeable fact.
Plain Objects
Sometimes you need to know whether a value is a plain object (created with {} or Object.create(null)), as opposed to an array, a date, a class instance or null. Check its prototype:
function isPlainObject(value) {
if (value === null || typeof value !== "object") return false;
const proto = Object.getPrototypeOf(value);
return proto === Object.prototype || proto === null;
}
class Account {}
console.log(isPlainObject({ a: 1 }));
console.log(isPlainObject(Object.create(null)));
console.log(isPlainObject([]));
console.log(isPlainObject(new Date()));
console.log(isPlainObject(new Account()));true
true
false
false
falseThis matters for code that merges settings or clones data, where treating a Date or a class instance as a plain bag of properties would damage it.
In Practice
The Fee Tracker validates an imported payment record field by field, choosing the right check for each type and reporting every problem:
function validatePayment(p) {
const problems = [];
if (typeof p !== "object" || p === null || Array.isArray(p)) return ["not a record"];
if (typeof p.name !== "string" || p.name.trim() === "") problems.push("name must be text");
if (!Number.isFinite(p.amount) || p.amount <= 0) problems.push("amount must be a positive number");
if (!Number.isInteger(p.grade)) problems.push("grade must be a whole number");
if (p.paidOn !== undefined && !(p.paidOn instanceof Date)) problems.push("paidOn must be a Date");
return problems;
}
console.log(validatePayment({ name: "Navika", amount: 120, grade: 9 }));
console.log(validatePayment({ name: "", amount: "120", grade: 9.5, paidOn: "2026-01-05" }));
console.log(validatePayment([1, 2]));[]
['name must be text', 'amount must be a positive number', 'grade must be a whole number', 'paidOn must be a Date']
['not a record']Common Mistakes
typeof x === "object" to mean "a real object". It also matches null and arrays.
typeof x === "array". That string never appears. Use Array.isArray.
The global isNaN. It converts first. Use Number.isNaN.
instanceof on primitives. Always false. Use typeof for primitives.
Practice
Challenge: Write typeName(value) that returns "null", "array", "date", "map", "set", or otherwise the typeof result.
Show solution
function typeName(value) {
if (value === null) return "null";
if (Array.isArray(value)) return "array";
if (value instanceof Date) return "date";
if (value instanceof Map) return "map";
if (value instanceof Set) return "set";
return typeof value;
}
console.log([null, [], new Date(), new Map(), new Set(), 1n, () => {}].map(typeName));['null', 'array', 'date', 'map', 'set', 'bigint', 'function']Key Takeaways
| Question | Check |
|---|---|
| String? | typeof x === "string" |
| Usable number? | Number.isFinite(x) |
| Null? | x === null |
| Array? | Array.isArray(x) |
| Instance of a class? | x instanceof Account |
| Function? | typeof x === "function" |
Was this tutorial helpful?