Lingua-e
← Errors
JavaScript

Destructuring Undefined: What It Means and How to Fix It

Error reference

Destructuring Undefined

Language

JavaScript

Severity

High

When it happens

This TypeError occurs when you try to destructure properties from a value that is null or undefined.

Potential fixes

Provide a fallback with nullish coalescing: const { x } = getValue() ?? {}. Add a default value in the function signature: function fn({ x = 0 } = {}). Always initialize state to an empty object rather than undefined when awaiting async data.

Deep dive

This TypeError occurs when you try to destructure properties from a value that is null or undefined. Destructuring requires the right-hand side to be an object or array; null and undefined cannot be destructured.

Why it happens

  • •A function or API call returns undefined when an object was expected.
  • •An async data fetch has not resolved yet and the state is still undefined.
  • •A conditional return path leaves the value undefined in some cases.
  • •Destructuring from an optional function parameter that was not provided.

Potential fixes

Provide a fallback with nullish coalescing: const { x } = getValue() ?? {}. Add a default value in the function signature: function fn({ x = 0 } = {}). Always initialize state to an empty object rather than undefined when awaiting async data.

Examples

JavaScript

Code that triggers the error

function getUser() {
  return undefined;
}
const { name, email } = getUser();

Error output

TypeError: Cannot destructure property 'name' of undefined as it is undefined.

Fixed code

function getUser() {
  return undefined;
}
const { name, email } = getUser() ?? {};
console.log(name); // undefined (no throw)

Practice in English

How would you explain a Destructuring Undefined to a fellow dev? Choose the right phrase:

"I hit an error...

Debugging in English?

You just read real developer English to fix this. Lingua-e has free exercises built the same way: standups, code reviews, and real workplace scenarios.

Practice developer English, free