Back to JavaScript
2026-04-136 min read

JS Destructuring (JavaScript)

Learn JS Destructuring (JavaScript) step by step with clear examples and exercises.

Why This Matters

Destructuring is a powerful feature in JavaScript that simplifies working with complex data structures like arrays and objects by allowing you to extract specific pieces of data directly into variables. It's essential for writing cleaner, more efficient code in projects and interviews. In this guide, we will delve deeper into the core concept of destructuring, provide a worked example, common mistakes to avoid, practice questions, and frequently asked questions.

Prerequisites

Before diving into destructuring, it's essential to have a solid understanding of the following:

  • Variables and data types in JavaScript
  • Arrays and objects in JavaScript
  • ES6 syntax (if you're not familiar with it, check out our guide on ES6 Features)

Core Concept

Array Destructuring

Array destructuring allows you to extract elements from an array and assign them to variables using a syntax similar to the spread operator (...). Here's an example:

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

In this example, we're destructuring the array arr and assigning its elements to variables a, b, and c. If you have more elements in your array than variables, any remaining variables will be assigned undefined.

Nested Array Destructuring

You can also perform nested array destructuring by accessing sub-arrays:

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

Object Destructuring

Object destructuring works similarly but allows you to extract properties from an object. Here's an example:

const obj = { a: 1, b: 2, c: 3 };
const { a, b, c } = obj;
console.log(a, b, c); // Output: 1 2 3

In this example, we're destructuring the object obj and assigning its properties to variables with the same names. If you have more properties in your object than variables, any remaining properties will be ignored.

Object Shorthand Syntax

You can also create objects using object destructuring and shorthand property names:

const obj = { a, b };
obj.a = 1;
obj.b = 2;
console.log(obj); // Output: { a: 1, b: 2 }

Destructuring with Default Values

You can also provide default values for variables when destructuring objects:

const obj = { a: 1, b: 2 };
const { c = 0, d = '' } = obj;
console.log(c, d); // Output: 2 '' (since `c` is assigned the value of property `b`, and `d` is assigned an empty string as its default value)

Destructuring with Renamed Variables

You can rename variables when destructuring by assigning new names to extracted properties:

const obj = { firstName: 'John', lastName: 'Doe' };
const { firstName: fName, lastName: lName } = obj;
console.log(fName, lName); // Output: John Doe

Worked Example

Let's say you have a function that returns an array of objects representing user data:

function getUsers() {
return [
{ id: 1, name: 'Alice', age: 25 },
{ id: 2, name: 'Bob', age: 30 },
{ id: 3, name: 'Charlie', age: 35 }
];
}

You can use array destructuring to extract the user data and assign it to variables:

const users = getUsers();
const [user1, user2, user3] = users;
console.log(user1.name, user1.age); // Output: Alice 25
console.log(user2.name, user2.age); // Output: Bob 30
console.log(user3.name, user3.age); // Output: Charlie 35

But what if you want to extract only specific properties from each user object? You can do this by destructuring the objects directly:

const [ { name: user1Name, age: user1Age }, { name: user2Name, age: user2Age }, { name: user3Name, age: user3Age } ] = users;
console.log(user1Name, user1Age); // Output: Alice 25
console.log(user2Name, user2Age); // Output: Bob 30
console.log(user3Name, user3Age); // Output: Charlie 35

Common Mistakes

  1. Missing the equals sign (=) when destructuring objects:
const { a } = {}; // SyntaxError: Unexpected token '{'
  1. Destructuring an array with more elements than variables:
const arr = [1, 2, 3, 4];
const [a, b] = arr; // `c` will be assigned `undefined`
console.log(a, b, c); // Output: 1 2 undefined
  1. Trying to destructure a non-array or non-object:
const str = 'Hello';
const [a] = str; // TypeError: Cannot destructure property '0' of values '[h, e, l, ...]' as it is not an array.
  1. Using destructuring in a function without proper argument names:
function sum({ a, b }) {
return a + b;
}
const obj = { a: 1, b: 2 };
console.log(sum(obj)); // Output: 3
const [a, b] = Object.entries(obj);
console.log(sum([a, b])); // TypeError: Cannot destructure property '0' of 'Object.entries(...)' as it is not an array.
  1. Destructuring nested objects without proper path:
const obj = { data: { name: 'John', age: 30 } };
const { name, age } = obj; // SyntaxError: Cannot destructure property 'name' of 'obj' as it is a read-only array.
const { data: { name, age } } = obj; // Correct syntax

Common Mistakes (Expanded - Practice)

  1. Using destructuring with an empty object:
const { a } = {}; // SyntaxError: Unexpected token '{'
  1. Destructuring an array with fewer variables than elements:
const arr = [1, 2, 3];
const [a] = arr;
console.log(b); // ReferenceError: b is not defined
  1. Trying to destructure a non-array or non-object without default values:
const str = 'Hello';
const [a] = str; // TypeError: Cannot destructure property '0' of values '[h, e, l, ...]' as it is not an array.
  1. Using destructuring with a function without providing arguments:
function sum({ a, b }) {
return a + b;
}
console.log(sum()); // TypeError: Cannot read properties of undefined (reading 'a')
  1. Destructuring nested objects with incorrect path:
const obj = { data: { name: 'John', age: 30 } };
const { name, age } = obj; // SyntaxError: Cannot destructure property 'name' of 'obj' as it is a read-only array.
const { data: { name, age } } = obj; // Correct syntax

Practice Questions

  1. Write a function that takes an array of numbers and destructures the first two elements into variables a and b, then returns their sum.
function sumFirstTwo(arr) {
const [a, b] = arr;
return a + b;
}
console.log(sumFirstTwo([1, 2, 3])); // Output: 3
  1. Given the following object, write code to extract the property values for name, age, and city using destructuring and assign them to variables.
const person = { name: 'John', age: 30, city: 'New York' };
const { name, age, city } = person;
console.log(name, age, city); // Output: John 30 New York
  1. Write a function that takes an array of objects representing user data and returns the names and ages of users older than 25.
function getOlderUsers(users) {
return users.filter(({ age }) => age > 25).map(({ name, age }) => `${name} (${age})`);
}
const users = [
{ id: 1, name: 'Alice', age: 25 },
{ id: 2, name: 'Bob', age: 30 },
{ id: 3, name: 'Charlie', age: 35 }
];
console.log(getOlderUsers(users)); // Output: ['Bob (30)', 'Charlie (35)']
  1. Write a function that takes an array of objects representing user data and returns the total age of all users.
function getTotalAge(users) {
return users.reduce((total, { age }) => total + age, 0);
}
const users = [
{ id: 1, name: 'Alice', age: 25 },
{ id: 2, name: 'Bob', age: 30 },
{ id: 3, name: 'Charlie', age: 35 }
];
console.log(getTotalAge(users)); // Output: 90
  1. Write a function that takes an object representing a shopping cart and destructures the quantities of each item into a new array.
function getItemQuantities(cart) {
const quantities = Object.values(cart).map(({ quantity }) => quantity);
return quantities;
}
const cart = { apple: { quantity: 3 }, banana: { quantity: 2 }, orange: { quantity: 1 } };
console.log(getItemQuantities(cart)); // Output: [3, 2, 1]

FAQ

  1. Can I use destructuring with ES5 syntax?

Yes, you can use destructuring in ES6 and above. To use it in older versions of JavaScript, you'll need a transpiler like Babel.

  1. What happens if I try to destructure an empty array or object?

Attempting to destructure an empty array or object will result in an error (TypeError) unless you provide default values for the variables, as shown in the Core Concept section.

  1. Can I use destructuring with functions' parameters?

Yes! Function parameter destructuring allows you to extract arguments from a function call and assign them to variables:

function greet({ name }) {
console.log(`Hello, ${name}!`);
}

greet({ name: 'Alice' }); // Output: Hello, Alice!
  1. Can I use destructuring with the rest parameter (...) to extract all remaining elements or properties?

Yes! By using the rest parameter in combination with destructuring, you can extract all remaining elements from an array or properties from an object. Here's an example:

const arr = [1, 2, 3, 4];
const [a, b, ...rest] = arr;
console.log(a, b, rest); // Output: 1 2 [3, 4]
JS Destructuring (JavaScript) | JavaScript | XQA Learn