Checking Types

Updated · 6 min read
mediumES2026javascripttypeofinstanceoftype-checking

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 x works for primitives and functions. Quirks: typeof null is "object", and arrays are "object".
  • Array.isArray(x) checks for arrays; x === null checks for null.
  • x instanceof Class checks whether an object was made by a class or constructor (by walking its prototype chain).
  • Number.isNaN, Number.isFinite, Number.isInteger check numbers without converting them.
  • Object.prototype.toString.call(x) gives a precise tag like "[object Date]" for any value.

typeof

Valuetypeof
"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"
typeof.js▶ Try it
console.log(typeof NaN);
console.log(typeof null);
console.log(typeof []);
console.log(typeof class Account {});
console.log(typeof undeclaredVariable);
Output
number
object
object
function
undefined

Explanation: 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

null-array.js▶ Try it
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));
Output
null
array of 2
object
number

Check 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?"

instanceof.js▶ Try 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);
Output
true
true
true
true
true
false

Explanation: 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:

numbers.js▶ Try it
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));
Output
true false
true false
true false false
false true
QuestionUse
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:

tag.js▶ Try it
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()));
Output
[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:

plain.js▶ Try it
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()));
Output
true
true
false
false
false

This 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:

validate.js▶ Try it
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]));
Output
[]
['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
type-name.js▶ Try it
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));
Output
['null', 'array', 'date', 'map', 'set', 'bigint', 'function']

Quick check: Checking types

3 questions
1Which check is true for arrays and false for plain objects?
2What does Number.isNaN("hello") return?
3What does "text" instanceof String return?

Key Takeaways

QuestionCheck
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?

Lesson 51 of 139