Nearby lessons

9 of 22

JavaScript: Destructuring Arrays

πŸ“Œ What is Array Destructuring?

Array destructuring in JavaScript is a concise way to extract values from arrays and assign them to variables. It simplifies code and improves readability, especially when dealing with arrays with multiple elements or nested arrays.

1️⃣ Traditional vs Destructuring Syntax

Without destructuring, each value must be accessed individually using indexes. With destructuring, values are unpacked directly into variables.

Code Example
PREVIEW READY
Loading Editor...
Live Preview

2️⃣ Using Default Values

You can assign default values in case a value is undefined in the array.

Code Example
PREVIEW READY
Loading Editor...
Live Preview

3️⃣ Destructuring Nested Arrays

Destructuring works on nested arrays too by using nested patterns.

Code Example
PREVIEW READY
Loading Editor...
Live Preview

4️⃣ Using Rest Operator in Destructuring

The rest operator (...) can be used to collect remaining values into a single array.

Code Example
PREVIEW READY
Loading Editor...
Live Preview

5️⃣ Destructuring Parameters in Functions

You can directly destructure an array passed to a function inside its parameter list.

Code Example
PREVIEW READY
Loading Editor...
Live Preview

6️⃣ Returning and Destructuring Array from Function

Destructuring is helpful when functions return arrays. You can extract each item directly.

Code Example
PREVIEW READY
Loading Editor...
Live Preview

🧠 Test Your Knowledge

3 Questions

Progress: 0 / 3
Keep Going!AJS - Destructuring Object