Skip to content

TypeError

In JavaScript, TypeError is the native error type that the engine throws when code uses a value in a way its type doesn’t allow, such as calling a number or reading a property of undefined. It extends Error, so it carries a message, a name, and a stack trace. Reading a setting from a config object that’s null throws it:

Language: JavaScript
> const settings = null;
undefined

> settings.theme
Uncaught TypeError: Cannot read properties of null (reading 'theme')

When JavaScript Throws TypeError

The engine throws TypeError at run time, never while it parses the code. ECMAScript uses it for any failed operation that no other native error type describes. The usual triggers, most common first, are these:

  • Reading or writing a property of undefined or null
  • Calling a value that isn’t a function, or using new with a value that isn’t a constructor
  • Looping over a value that isn’t iterable with for...of, spread syntax, or array destructuring
  • Assigning to a const binding, or to a read-only property in strict mode
  • Mixing BigInt values and numbers in arithmetic

Code also throws it on purpose to reject an argument of the wrong type: throw new TypeError("Expected a string"). The constructor takes the same arguments as Error and works with or without new.

Catching TypeError

Say a plugin set its onSave hook to something other than a function. A try statement can skip the broken hook and rethrow any other error:

Language: JavaScript
> const hooks = { onSave: "not a function" };
undefined

> try {
…   hooks.onSave();
… } catch (error) {
…   if (!(error instanceof TypeError)) throw error;
…   `Skipped hook: ${error.message}`;
… }
'Skipped hook: hooks.onSave is not a function'

The instanceof check fails for errors from another realm, such as an iframe or a node:vm context. Checking error.name === "TypeError" still works there.

Common Messages

V8, the engine in Chrome and Node.js, prints these TypeError messages most often:

Message (V8) Usual Cause Fix
Cannot read properties of undefined (reading 'x') Reading a property of a missing value, such as an absent key Trace where the value comes from, or guard with ?.
Cannot read properties of null (reading 'x') Reading a property of null, often a querySelector() miss Fix the selector, or check for null first
x is not a function Calling a misspelled method or a non-function value Fix the name, or check the value before the call
x is not iterable Using for...of or spread on a plain object or undefined Loop over Object.entries(x), or pass an array
Assignment to constant variable. Reassigning a const binding Declare the binding with let if it must change
Cannot set properties of null (setting 'x') Writing to a property of null, often a missing element Make sure the object exists first
Cannot convert undefined or null to object Passing undefined or null to Object.keys() or Object.entries() Pass an object, or default with ?? {}
x is not a constructor Using new with an arrow function or the wrong import Drop new, or import the class
Converting circular structure to JSON Passing a self-referencing object to JSON.stringify() Remove the cycle, or use a replacer
fetch failed Node.js: a network error in fetch(). Chrome prints Failed to fetch, which also covers CORS blocks Check the URL and server, and read error.cause

Firefox and Safari word several of these messages differently:

V8 Firefox (SpiderMonkey) Safari (JavaScriptCore)
Cannot read properties of undefined (reading 'x') can't access property "x", y is undefined undefined is not an object (evaluating 'y.x')
Cannot set properties of null (setting 'x') can't access property "x", y is null null is not an object (evaluating 'y.x = z')
Assignment to constant variable. invalid assignment to const 'x' Attempted to assign to readonly property.
Converting circular structure to JSON cyclic object value JSON.stringify cannot serialize cyclic structures.

Before V8 9.3, which shipped in Chrome 93 and Node.js 16.9, the first row read Cannot read property 'x' of undefined. For the top two rows, the lasting fix is to find where the undefined or null came from, since a guard only hides it.

A developer diagnoses warning cards using a machine labelled TRY, CATCH and FINALLY.

Course

JavaScript Error Handling in Depth for Pythonistas

Port a food bank's all-or-nothing donation import from Python to JavaScript, and learn to define error classes, chain errors with cause, report many errors at once, and use a debugger.

intermediate js py

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Leodanis Pozo Ramos • Updated Nov. 12, 2026