Everything, Everywhere
Verified Specification | Standardized Formulas | Instant Precision
Secure & Private (Zero Data Retention) Free Access • No Sign-Up

Destructuring

Destructuring allows you to extract data from arrays and objects into separate variables in a single step.

Try It Yourself

      
📋 Copy JavaScript Nested Destructuring Pattern Snippet
// Nested Object & Array Destructuring with Defaults
const response = {
  data: { user: { id: 101, full_name: "Neo" } },
  status: 200
};

const {
  data: { user: { full_name: name = "Guest" } } = {},
  status: statusCode
} = response;

⚠️ 5 Fatal Traps & JavaScript Pitfalls

Trap #1: Destructuring from null or undefined TypeError
Attempting to destructure properties from null or undefined throws an immediate fatal TypeError: Cannot destructure property ... of null.
Trap #2: Default Values Only Trigger on undefined, NOT null
In const { name = "Default" } = { name: null };, name remains null! Default values only apply when the value is strictly undefined.
Trap #3: Renaming Syntax vs Type Annotation Confusion
In const { id: userId } = obj;, userId is the new variable name, NOT a TypeScript type annotation.
Trap #4: Destructuring Destroys Immutability for Nested References
Destructuring extracts shallow pointers. Modifying a property on a destructured nested object mutates the source object.
Trap #5: Array Destructuring Out-of-Bounds Indexing
Destructuring past the end of an array (const [a, b, c] = [1];) silently sets missing variables to undefined without throwing an error.

💬 Frequently Asked Questions

How do you swap two variables without a temporary variable in JavaScript?
Use array destructuring assignment: [a, b] = [b, a];.
How do you rename a variable during object destructuring?
Use the colon syntax: const { originalKey: newVariableName } = object;.
What is rest property destructuring in objects?
const { id, ...details } = user; assigns id to its own variable and collects all remaining properties into a new object called details.
Can you destructure function parameters directly?
Yes: function render({ title, width = 100 }) { ... } unpacks object properties directly in the function signature.
How do you ignore items during array destructuring?
Leave empty commas: const [first, , third] = [1, 2, 3]; skips the second item.
Sponsored Utility
While You're Here
Sponsored Recommendations
Advertisement