TypeError

Thrown when an operation is attempted on a value that doesn't support it — reading a property of null, calling something that isn't a function.

                new TypeError(message)
              

Purpose

A TypeError means the code referenced a real value — the name existed and held something — but that value’s type doesn’t support the operation attempted on it.

Example

const total = null;
console.log(total.toFixed(2));
// TypeError: Cannot read properties of null (reading 'toFixed')

const notAFunction = 5;
notAFunction();
// TypeError: notAFunction is not a function

Common causes

  • Reading a property or calling a method on null or undefined — often because a variable wasn’t set the way you expected (a missing object key, a function that returned nothing).
  • Calling a value that isn’t a function.
  • Reassigning a const binding, or writing to a read-only or non-writable property in strict mode.
  • Passing the wrong type to a built-in that expects a specific one (spreading a non-iterable value, for example).

Distinguishing it from other errors

TypeError means the value exists but doesn’t support what you tried to do with it. Compare with ReferenceError, which means the name itself doesn’t exist. total.toFixed(2) throwing TypeError (not ReferenceError) is the tell that total was found — it’s just null.

Runtime notes

TypeError is part of ECMAScript, so the error type is standard everywhere; the exact wording of the message is engine-specific.

Taught in these lessons

Related reference