Hello! In our last lesson, we explored how template literals help us elegantly compose strings from variables. Now, we're going to look at the other side of the coin: how to efficiently deconstruct data structures to extract values into variables.
Today's lesson is all about destructuring assignment. This is a powerful ES6 feature that provides a concise syntax to "unpack" values from arrays or properties from objects into distinct variables. Your goal is to learn how to use destructuring assignment to extract values from objects and arrays. Mastering this will make your code significantly cleaner and is a fundamental pattern used constantly in modern frameworks like React and Next.js, especially when dealing with component properties (props).
The Problem: The "Old" Way of Extracting Data
Before we dive into the new syntax, let's look at how we would have extracted data before ES6. Imagine you have an object representing a user:
const user = {
fullName: "Jane Doe",
age: 30,
email: "jane.doe@example.com"
};
If you wanted to create variables for the name and age to use them separately, you would do this:
// The old, repetitive way
const fullName = user.fullName;
const age = user.age;
console.log(`Welcome, ${fullName}!`); // Welcome, Jane Doe!
console.log(`You are ${age} years old.`); // You are 30 years old.
This works, but it's repetitive. For each property you need, you have to write a new line, repeating user. every time. Destructuring provides a much better way.
Array Destructuring: Unpacking by Position
Let's start with arrays. Array destructuring is position-based. This means the first variable you declare gets the value of the first element in the array, the second variable gets the second element, and so on.
The syntax uses square brackets [] on the left side of the assignment.

// The array we want to destructure
const userDetails = ["John", "Doe", 30];
// The "old" way
const firstNameOld = userDetails[0];
const lastNameOld = userDetails[1];
// The new destructuring way
const [firstName, lastName, age] = userDetails;
console.log(firstName); // "John"
console.log(lastName); // "Doe"
console.log(age); // 30
Notice how one line of code accomplishes what previously took three.
To see this in action and explore more advanced array destructuring capabilities, let's watch a short segment from a helpful video.
Why Is Array/Object Destructuring So Useful And How To Use It
This video from Web Dev Simplified provides a clear, practical demonstration of array destructuring. We'll watch a few clips to see how it works and what else you can do with it.
Watch from 00:46 to 03:05. Pay close attention to: The basic syntax for pulling out the first few elements. How to skip an element using an empty comma (,). How to use the rest operator (...) to collect all remaining elements into a new array.
As the video showed, you have a lot of flexibility:
-
Skipping Elements: If you only need the first and third elements, you can use a comma to skip the second one.
const numbers = [10, 20, 30, 40]; const [first, , third] = numbers; console.log(first); // 10 console.log(third); // 30 -
The Rest Pattern: This is incredibly useful. You can grab the elements you need and then collect all the rest of them into a new array.
const scores = [98, 85, 76, 60, 55]; const [topScore, secondScore, ...otherScores] = scores; console.log(topScore); // 98 console.log(secondScore); // 85 console.log(otherScores); // [76, 60, 55] -
Default Values: What happens if you try to destructure an element that doesn't exist? The variable becomes
undefined. You can provide a default value to be used in this case with the=operator.const config = ["dark"]; const [theme, layout = "grid"] = config; console.log(theme); // "dark" (from the array) console.log(layout); // "grid" (the default value is used)
For a deeper dive into these concepts with more examples, the following article is an excellent resource.
The javascript.info tutorial provides a comprehensive text-based explanation of destructuring. It's a great resource to solidify your understanding.
Read the introduction and the section named 'Array destructuring'. This covers the core concepts we've discussed, plus a few other neat tricks like swapping variables.
Object Destructuring: Unpacking by Name
Object destructuring is even more common and powerful. Unlike arrays, it is name-based, not position-based. This means you use curly braces {} and specify the names of the properties (keys) you want to extract. The variable names must match the property names.
const user = {
fullName: "Jane Doe",
age: 30,
};
// The order doesn't matter, because it's based on name!
const { age, fullName } = user;
console.log(fullName); // "Jane Doe"
console.log(age); // 30
Let's return to the video to see how object destructuring works.
Why Is Array/Object Destructuring So Useful And How To Use It
Now let's see how this works for objects. Pay close attention to the syntax differences and the extra features available.
Watch from 06:05 to 08:43. Focus on: The basic syntax using curly braces {}. How to rename a variable during destructuring (e.g., name: firstName). This is also called aliasing. How to set default values for properties that might not exist. How the rest operator (...) works with objects.
Let's summarize those key features:
-
Renaming (Aliasing): If you want to extract a property into a variable with a different name, use a colon (
:).const product = { id: "abc-123", price: 59.99 }; // Extracts 'id' into a variable named 'productId' const { id: productId, price } = product; console.log(productId); // "abc-123" console.log(price); // 59.99 -
Default Values: Works just like with arrays. If a property doesn't exist on the object, the default value is used.
const settings = { theme: "dark" }; const { theme, notifications = true } = settings; console.log(theme); // "dark" console.log(notifications); // true (default value)
Test your understanding!
Given the following book object, use destructuring to create three variables:
title(which should contain the value of thetitleproperty).authorName(which should contain the value of theauthorproperty).pages(which should be200since it's not in the object).
const book = {
title: "The Art of Code",
author: "John Smith"
};
Show answer
const book = {
title: "The Art of Code",
author: "John Smith"
};
const { title, author: authorName, pages = 200 } = book;
console.log(title); // "The Art of Code"
console.log(authorName); // "John Smith"
console.log(pages); // 200
Explanation:
titledirectly extracts thetitleproperty into a variable of the same name.author: authorNameextracts theauthorproperty and renames it to a new variable calledauthorName.pages = 200attempts to extract thepagesproperty. Since it doesn't exist on thebookobject, the default value of200is used.
Powerful Use Cases for Your Next.js Journey
Destructuring truly shines when you combine these concepts or use them in more complex scenarios. Two of the most important are nested destructuring and destructuring function parameters.
1. Nested Destructuring
Real-world data is often nested. Destructuring allows you to reach deep into an object or array to pull out exactly what you need. The pattern on the left side of the assignment should mirror the structure of the data on the right.
Why Is Array/Object Destructuring So Useful And How To Use It
Let's watch one last clip to see how to destructure nested objects, which you will encounter all the time when working with data from APIs.
Watch from 08:36 to 09:40. Observe how the destructuring pattern on the left mirrors the structure of the object on the right to extract a nested property like city.
Here's an example:
const user = {
id: 1,
name: "Leanne Graham",
address: {
street: "Kulas Light",
city: "Gwenborough",
}
};
// Extracting the nested 'city' property
const { address: { city } } = user;
console.log(city); // "Gwenborough"
2. Destructuring in Function Parameters
This is perhaps the most important use case for your goal of learning Next.js. In React and Next.js, components receive data as an object called props. Destructuring in the function's parameter list is the standard way to unpack these props.
Why Is Array/Object Destructuring So Useful And How To Use It
This final clip shows what is arguably the most common use case for object destructuring in modern JavaScript development: unpacking function arguments.
Watch from 10:35 to 12:26. This is a crucial concept. Notice how destructuring the user object directly in the function parameters makes the function body cleaner and more explicit about what properties it needs.
Let's compare the before and after:
const person = {
name: "Kyle",
age: 24,
};
// Without destructuring
function printUserOld(user) {
console.log(`Name: ${user.name}, Age: ${user.age}`);
}
// With destructuring in the parameters
function printUserNew({ name, age }) {
console.log(`Name: ${name}, Age: ${age}`);
}
printUserOld(person); // Name: Kyle, Age: 24
printUserNew(person); // Name: Kyle, Age: 24
The second version is preferred because it's more declarative. The function signature immediately tells any developer reading the code exactly which properties from the person object it uses.
Conclusion
You've learned how destructuring provides a concise and powerful syntax for extracting data from arrays and objects. It makes your code more readable, less repetitive, and is a cornerstone of modern JavaScript development.
Key Takeaways:
- Destructuring is a way to "unpack" values from data structures.
- Array destructuring
[...]is positional. - Object destructuring
{...}is name-based. - Both can use default values (
=) for missing items and the rest pattern (...) to collect remaining items. - Object destructuring allows renaming properties with the colon syntax (
key: newName). - A key use case is destructuring arguments directly in a function's parameter list.

Next Up
You've now seen the ... syntax used as a "rest" pattern to collect remaining items during destructuring. In the next lesson, we'll explore its other identity: the spread operator. You'll learn how to use ... to expand elements from arrays and objects into other data structures, which is the perfect complement to what you've learned today.