Skip to content

Latest commit

 

History

History
191 lines (133 loc) · 4.13 KB

Destructuring-assignment.md

File metadata and controls

191 lines (133 loc) · 4.13 KB

How Does Destructuring Assignment Work in JavaScript ?

Destructuring assignment in JavaScript is a concise syntax that allows you to extract values from arrays or properties from objects and assign them to variables. This feature is particularly useful for cleaner and more readable code.


1. Destructuring Arrays

Destructuring arrays allows you to unpack values from an array into separate variables.

Syntax:

const arr = [1, 2, 3];
const [a, b, c] = arr;
console.log(a, b, c); // Output: 1 2 3

Skipping Elements:

const arr = [1, 2, 3, 4];
const [first, , third] = arr;
console.log(first, third); // Output: 1 3

Default Values:

If a value is undefined, you can assign a default value.

const arr = [1];
const [a, b = 2] = arr;
console.log(a, b); // Output: 1 2

2. Destructuring Objects

Destructuring objects allows you to unpack properties into variables using their property names.

Syntax:

const person = { name: "Alice", age: 25 };
const { name, age } = person;
console.log(name, age); // Output: Alice 25

Renaming Variables:

You can rename variables while destructuring.

const person = { name: "Alice", age: 25 };
const { name: fullName, age: years } = person;
console.log(fullName, years); // Output: Alice 25

Default Values:

Assign default values for properties that might be undefined.

const person = { name: "Alice" };
const { name, age = 30 } = person;
console.log(name, age); // Output: Alice 30

3. Nested Destructuring

You can destructure nested arrays and objects.

For Arrays:

const arr = [1, [2, 3]];
const [a, [b, c]] = arr;
console.log(a, b, c); // Output: 1 2 3

For Objects:

const person = { name: "Alice", address: { city: "Wonderland", zip: 12345 } };
const {
  address: { city, zip },
} = person;
console.log(city, zip); // Output: Wonderland 12345

4. Combined Destructuring

You can destructure arrays and objects together.

const data = { id: 1, info: [10, 20, 30] };
const {
  id,
  info: [first, second],
} = data;
console.log(id, first, second); // Output: 1 10 20

5. Destructuring in Function Parameters

Destructuring is useful for extracting values directly in function parameters.

For Arrays:

function sum([a, b]) {
  return a + b;
}
console.log(sum([1, 2])); // Output: 3

For Objects:

function greet({ name, age }) {
  return `Hello, ${name}. You are ${age} years old.`;
}
console.log(greet({ name: "Alice", age: 25 })); // Output: Hello, Alice. You are 25 years old.

6. Rest Pattern with Destructuring

The rest pattern collects the remaining elements or properties into a separate variable.

For Arrays:

const arr = [1, 2, 3, 4];
const [a, b, ...rest] = arr;
console.log(a, b, rest); // Output: 1 2 [3, 4]

For Objects:

const person = { name: "Alice", age: 25, country: "Wonderland" };
const { name, ...others } = person;
console.log(name, others); // Output: Alice { age: 25, country: 'Wonderland' }

7. Use Cases of Destructuring

  • Extracting API Response Data: Simplify working with nested response data.
  • Swapping Variables:
let a = 1,
  b = 2;
[a, b] = [b, a];
console.log(a, b); // Output: 2 1
  • Assigning Variables from Arrays:
const rgb = [255, 100, 50];
const [red, green, blue] = rgb;
console.log(`Red: ${red}, Green: ${green}, Blue: ${blue}`);
  • Providing Default Parameters in Functions:
function createUser({ name = "Guest", isAdmin = false } = {}) {
  return { name, isAdmin };
}
console.log(createUser()); // Output: { name: 'Guest', isAdmin: false }

Conclusion

Destructuring assignment is a powerful feature in JavaScript that simplifies variable assignment and improves code readability. Whether you're working with simple arrays or deeply nested objects, destructuring makes it easier to extract the data you need.