🚀 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...

Array Destructuring

AI & DATA SCIENCE // array-destructuring

Array destructuring extracts values from arrays into variables. Supports skipping, rest elements, and defaults.

Syntax

const [a, b, c] = [1, 2, 3];
const [first, , third] = [1, 2, 3]; // skip second
const [x = 10] = [];  // default value

Deep Dive Course

**Destructuring** unpacks array values into distinct variables using pattern matching. It's particularly powerful for: swapping variables, function return values, ignoring elements (using `,`), and collecting the rest (`...rest`).

1Understanding Array Destructuring

Destructuring unpacks array values into distinct variables using pattern matching. It's particularly powerful for: swapping variables, function return values, ignoring elements (using ,), and collecting the rest (...rest).

💡

Swap variables elegantly with destructuring: [a, b] = [b, a] — no temporary variable needed.

editor.html
// Basic destructuring
const [name, age, city] = ['Alice', 30, 'NYC'];
console.log(name, age, city); // Alice 30 NYC

// Swap variables
let x = 1, y = 2;
[x, y] = [y, x];
console.log(x, y); // 2 1

// Skip elements
const [,, third] = [10, 20, 30];
console.log(third); // 30
localhost:3000

2Practical Example

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

editor.html
// Rest + defaults
const [head, ...tail] = [1, 2, 3, 4, 5];
console.log(head); // 1
console.log(tail); // [2, 3, 4, 5]

// Destructure function return
function minMax(arr) {
  return [Math.min(...arr), Math.max(...arr)];
}
const [min, max] = minMax([3, 1, 7, 2, 9]);
console.log(min, max); // 1 9
localhost:3000

3Best Practices

Follow these guidelines when working with Array Destructuring:

1. Use rest (...rest) to collect remaining elements

2. Provide default values for potentially undefined elements

3. Use destructuring in function parameters for clarity

⚠️

Tip: Swap variables elegantly with destructuring: [a, b] = [b, a] — no temporary variable needed.

editor.html
// Basic destructuring
const [name, age, city] = ['Alice', 30, 'NYC'];
console.log(name, age, city); // Alice 30 NYC

// Swap variables
let x = 1, y = 2;
[x, y] = [y, x];
console.log(x, y); // 2 1

// Skip elements
const [,, third] = [10, 20, 30];
console.log(third); // 30
localhost:3000

Examples

Example 01Basic Usage
// Basic destructuring
const [name, age, city] = ['Alice', 30, 'NYC'];
console.log(name, age, city); // Alice 30 NYC

// Swap variables
let x = 1, y = 2;
[x, y] = [y, x];
console.log(x, y); // 2 1

// Skip elements
const [,, third] = [10, 20, 30];
console.log(third); // 30
Example 02Advanced Example
// Rest + defaults
const [head, ...tail] = [1, 2, 3, 4, 5];
console.log(head); // 1
console.log(tail); // [2, 3, 4, 5]

// Destructure function return
function minMax(arr) {
  return [Math.min(...arr), Math.max(...arr)];
}
const [min, max] = minMax([3, 1, 7, 2, 9]);
console.log(min, max); // 1 9

Best Practices

  • Use rest (...rest) to collect remaining elements
  • Provide default values for potentially undefined elements
  • Use destructuring in function parameters for clarity

Interview Question

How would you swap two variables using destructuring?

Hint: No temp variable needed.

[a, b] = [b, a]. JavaScript creates a temporary array [b, a] on the right, then destructures it into [a, b] on the left. This is equivalent to: const temp = a; a = b; b = temp — but far more readable.

Exercises

MediumPractice using Array Destructuring in a real scenario.
View Solution
// Basic destructuring
const [name, age, city] = ['Alice', 30, 'NYC'];
console.log(name, age, city); // Alice 30 NYC

// Swap variables
let x = 1, y = 2;
[x, y] = [y, x];
console.log(x, y); // 2 1

// Skip elements
const [,, third] = [10, 20, 30];
console.log(third); // 30

Frequently Asked Questions

How would you swap two variables using destructuring?

[a, b] = [b, a]. JavaScript creates a temporary array [b, a] on the right, then destructures it into [a, b] on the left. This is equivalent to: const temp = a; a = b; b = temp — but far more readable.

Related Functions

DestructuringElements-AccessSpread OperatorRestParameters