🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEjavascript

javascript Documentation

LOADING ENGINE...

Destructuring

AI & DATA SCIENCE // destructuring

Destructuring extracts values from arrays and objects into variables using a pattern-matching syntax.

Syntax

// Object
const { name, age } = user;
// Array
const [first, second] = arr;
// Nested
const { address: { city } } = user;

Deep Dive Course

**Destructuring** is one of ES6's most useful features. It works for objects and arrays in variable declarations, function parameters, and `for...of` loops. Features include **defaults**, **renaming**, **nested** patterns, and **rest**. It's particularly powerful for working with API responses and function options.

1Understanding Destructuring

Destructuring is one of ES6's most useful features. It works for objects and arrays in variable declarations, function parameters, and for...of loops. Features include defaults, renaming, nested patterns, and rest. It's particularly powerful for working with API responses and function options.

💡

Destructure function parameters for named arguments: 'function create({ name, age = 18, role = "user" })' is much cleaner than positional arguments.

editor.html
// Object destructuring with defaults & renaming
const user = { name: 'Alice', role: 'admin' };
const { name: userName, role, age = 30 } = user;

console.log(userName); // 'Alice' (renamed)
console.log(role);     // 'admin'
console.log(age);      // 30 (default!)
localhost:3000

2Practical Example

Here is a real-world application of Destructuring showing how it is used in production JavaScript code.

editor.html
// Nested + function parameter destructuring
function renderProfile({ name, address: { city, country = 'Unknown' } }) {
  console.log(`${name} from ${city}, ${country}`);
}

renderProfile({
  name: 'Bob',
  address: { city: 'Paris', country: 'France' }
}); // Bob from Paris, France
localhost:3000

3Best Practices

Follow these guidelines when working with Destructuring:

1. Use defaults in destructuring to handle missing values

2. Destructure in function params for named arguments pattern

3. Use renaming syntax to avoid conflicts: { name: userName }

⚠️

Tip: Destructure function parameters for named arguments: 'function create({ name, age = 18, role = "user" })' is much cleaner than positional arguments.

editor.html
// Object destructuring with defaults & renaming
const user = { name: 'Alice', role: 'admin' };
const { name: userName, role, age = 30 } = user;

console.log(userName); // 'Alice' (renamed)
console.log(role);     // 'admin'
console.log(age);      // 30 (default!)
localhost:3000

Examples

Example 01Basic Usage
// Object destructuring with defaults & renaming
const user = { name: 'Alice', role: 'admin' };
const { name: userName, role, age = 30 } = user;

console.log(userName); // 'Alice' (renamed)
console.log(role);     // 'admin'
console.log(age);      // 30 (default!)
Example 02Advanced Example
// Nested + function parameter destructuring
function renderProfile({ name, address: { city, country = 'Unknown' } }) {
  console.log(`${name} from ${city}, ${country}`);
}

renderProfile({
  name: 'Bob',
  address: { city: 'Paris', country: 'France' }
}); // Bob from Paris, France

Best Practices

  • Use defaults in destructuring to handle missing values
  • Destructure in function params for named arguments pattern
  • Use renaming syntax to avoid conflicts: { name: userName }

Interview Question

How do you provide default values in destructuring?

Hint: = syntax in the pattern.

Use = in the destructuring pattern: const { name = 'Anonymous', age = 0 } = obj. The default is used when the property is undefined (not when it's null or false). For nested destructuring: const { address: { city = 'Unknown' } = {} } = user — the inner {} = {} provides a default for the address object itself.

Exercises

MediumPractice using Destructuring in a real scenario.
View Solution
// Object destructuring with defaults & renaming
const user = { name: 'Alice', role: 'admin' };
const { name: userName, role, age = 30 } = user;

console.log(userName); // 'Alice' (renamed)
console.log(role);     // 'admin'
console.log(age);      // 30 (default!)

Frequently Asked Questions

How do you provide default values in destructuring?

Use = in the destructuring pattern: const { name = 'Anonymous', age = 0 } = obj. The default is used when the property is undefined (not when it's null or false). For nested destructuring: const { address: { city = 'Unknown' } = {} } = user — the inner {} = {} provides a default for the address object itself.

Related Functions

Array-DestructuringSpread OperatorRestParametersVariables