Skip to content

Invalid Destructuring Pattern

Quick summary

Your destructuring pattern expects a different shape than the source — variables end up undefined or the parser rejects the pattern.

Why this happens

  • Array destructuring on an object, or object destructuring on an array

  • Nested destructuring that does not match the nested source shape

  • Default values that reference other destructured variables out of order

Minimal example

✗ Broken code
const user = { name: 'Ada', age: 30 };
const [name, age] = user; // wrong — array destruct on object
console.log(name); // undefined
✓ Fixed code
const user = { name: 'Ada', age: 30 };
const { name, age } = user; // object destructuring
console.log(name); // 'Ada'

Array destructuring expects an iterable, but user is a plain object, so name becomes undefined. Using object destructuring with matching keys extracts the properties correctly.

How to diagnose

  • Log each destructured variable to see which are undefined

  • Print the source value's shape and compare with the pattern

  • Check the SyntaxError if the pattern itself is malformed

How to fix

  • Match the destructuring style to the source ([] for arrays, {} for objects)

  • Add default values for properties that may be missing

  • Destructure in steps for deeply nested shapes to keep it readable

How to prevent

  • Print the source shape before writing the destructuring pattern

  • Add defaults for optional fields to avoid undefined surprises

Related Resources

Related Glossary

Related Lessons

← Back to language