TypeError in JavaScript: a value is not shaped as expected

A TypeError reports a value used in a way its type does not allow: the most common messages, what really causes them, and how to avoid them.
3 min read
Believemy logo

This is by far the most encountered error in JavaScript, and its message is often read the wrong way. It does not say the code is wrong, it says a value was not the one expected at that spot.

Put differently, the problem nearly always comes from somewhere else: from the line that produced the value, not the line that broke.


Definition

A TypeError is thrown when an operation targets a value whose type does not allow it: reading a property on null, calling something that is not a function, walking through something that cannot be walked through.

JAVASCRIPT
const user = null;

try {
  console.log(user.name);
} catch (err) {
  console.log(err.name);      // TypeError
  console.log(err.message);   // Cannot read properties of null (reading 'name')
}

The message names the offending value and the property that was asked for. That is already half the diagnosis: what remains is tracing back to where user took that value.


The messages you read the most

MessageWhat happened
Cannot read properties of undefinedA missing field, an incomplete response, an empty array
x is not a functionA typo, a missing import, a value that is not what you think
x is not iterableDestructuring or an of loop over something other than an array
Assignment to constant variableA reassignment of a const

Those four rows cover most of what shows up in production. The first case is by far the most frequent, and almost always tied to data received from the network.


Preventing them rather than catching them

A TypeError reports a defect to fix, not a situation to recover from. Two tools in the language remove the whole class of these errors.

JAVASCRIPT
const response = { profile: null };

// Breaks: Cannot read properties of null
// console.log(response.profile.name);

// Safe: optional chaining stops short and hands back undefined
console.log(response.profile?.name);   // undefined

// Safe, with a fallback value
console.log(response.profile?.name ?? "Anonymous");   // Anonymous

Optional chaining (?.) cuts short as soon as a step is null or undefined, and Nullish coalescing (??) supplies the fallback. Checking the shape of the data on the way in remains the sturdiest protection of all.

Good to know

Optional chaining belongs on genuinely optional values, not everywhere. Written by reflex on data that ought to exist, it turns a talkative TypeError into a silent undefined that will resurface three screens later.


Frequently asked questions

Question

How does it differ from a ReferenceError?

A ReferenceError is about a name that exists nowhere, a TypeError about a value that exists but does not fit. A variable never declared gives you the first, a declared variable holding undefined gives you the second the moment you read a property off it.


Question

Should TypeError be caught?

Rarely. A try placed around a TypeError hides a defect instead of fixing it, and the program carries on with inconsistent data. Reserve recovery for expected failures, network or user input, and fix the rest at the source.


Question

How do you find the origin of a message that stays vague?

By reading the error's call stack, which gives the run of functions it passed through. The reported line is the one that broke, but the cause often sits one or two entries below, where the value was built in the first place.

Related terms

Discover our javaScript glossary

Every word of JavaScript explained simply: keywords, built-in objects, methods, errors and concepts. Clear definitions and examples that actually run, to learn and to troubleshoot.

Share this article

Want to help us? Share this article on your networks or even better: on your site, in an article or in your newsletter.