Back to JavaScript Notes
Topic #353

The typeof Operator

By the end of this lesson, you will be able to use typeof to inspect a JavaScript value's runtime type and know when it is not enough.

What it is

typeof is a unary operator that returns a string describing the type of its operand. It is evaluated at runtime, so it can answer questions like “is this value a string?” or “is this value callable?” without throwing an error for most undeclared variables.

The common results are "undefined", "boolean", "number", "string", "bigint", "symbol", "function", and "object". Related terms include primitive type, reference type, type guard, and runtime type inspection.

Why it matters

  • It lets you validate function arguments before using them.
  • It can safely check whether a variable exists: typeof maybeMissing === "undefined".
  • It distinguishes callable values from non-callable values with typeof value === "function".
  • It helps debug unexpected values, such as seeing that NaN is still a "number".

Syntax or steps

typeof value
typeof(value)

Both forms do the same thing. The parentheses are optional and are useful when the operand is an expression.

  1. Choose the value you want to inspect.
  2. Apply typeof to it.
  3. Compare the returned string with the expected type.
  4. Use a more specific check when typeof is too broad.

Example

const values = [
  undefined,
  null,
  true,
  42,
  "hi",
  Symbol("id"),
  10n,
  function () {},
  [],
  {},
  NaN
];

for (const value of values) {
  console.log(typeof value, value);
}

Expected output

undefined undefined
object null
boolean true
number 42
string hi
symbol Symbol(id)
bigint 10n
function function () {}
object []
object {}
number NaN

The array values contains one example of each important runtime category. The loop prints the result of typeof value and then the value itself. Notice that null, arrays, and plain objects all report "object", while NaN reports "number".

Common mistakes

  • Assuming "object" means a plain object. Arrays and null also return "object". Use Array.isArray(value) for arrays and check value !== null for objects.
  • Forgetting that typeof null === "object". This is a long-standing JavaScript quirk. Always test for null separately.
  • Using typeof to detect NaN. typeof NaN is "number". Use Number.isNaN(value) instead.
  • Using typeof on a variable in the temporal dead zone. For example, typeof x can throw if x is declared with let or const but not yet initialized.

When to use it

Use typeof for broad, fast checks of primitives and functions. Use other tools when you need to distinguish among object-like values.

TaskBest checkWhy
Check a primitivetypeof value === "string"typeof reports primitive types directly.
Check an arrayArray.isArray(value)typeof [] is "object", not "array".
Check a class instancevalue instanceof MyClasstypeof only says "object" for instances.
Check a built-in tagObject.prototype.toString.call(value)It can distinguish [object Date], [object RegExp], and similar types.

Practice

Guided exercise: write a function that returns true only for plain objects.

function isPlainObject(value) {
  return typeof value === "object" && value !== null && !Array.isArray(value);
}

console.log(isPlainObject({}));
console.log(isPlainObject([]));
console.log(isPlainObject(null));

Expected output:

true
false
false

Challenge: write a function that returns "array" for arrays, "null" for null, and otherwise returns the result of typeof value. Hint: check arrays and null before the generic typeof result.

Quick check

Question: What does typeof [] return, and what should you use instead to detect arrays?

Answer: It returns "object". Use Array.isArray([]), which returns true.

Summary

typeof is a simple runtime tool for identifying primitive types and functions. Its main limitation is that many object-like values, including arrays and null, all report "object". Combine it with checks like Array.isArray, instanceof, and Object.prototype.toString.call when you need precise type detection.

Want to go beyond the notes?

Join Coding Now Tech Institute's JavaScript course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

The typeof Operator – FAQs

Quick answers about learning The typeof Operator in JavaScript.

This free note from Coding Now Tech Institute explains The typeof Operator in JavaScript — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every JavaScript topic on Coding Now Tech Institute, including The typeof Operator, is 100% free with no signup required.
With focused practice, most students grasp The typeof Operator in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now