codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🌐

JavaScript

51 / 65 topics
50JavaScript Template Literals51JavaScript Destructuring52JavaScript Spread & Rest Operators53JavaScript Default Parameters54JavaScript Modules (import/export)55JavaScript Iterators and Generators56JavaScript Proxies
Tutorials/JavaScript/JavaScript Destructuring
🌐JavaScript

JavaScript Destructuring

Updated 2026-05-12
15 min read

JavaScript Destructuring

Destructuring is a powerful feature in JavaScript that allows you to extract values from arrays or properties from objects into distinct variables. This can make your code cleaner, more readable, and easier to manage, especially when dealing with complex data structures.

In this tutorial, we'll explore how destructuring works, its syntax, common use cases, and some practical examples. By the end of this lesson, you'll be able to confidently use destructuring in your JavaScript projects.

Introduction

Destructuring is a way to unpack values from arrays or properties from objects into variables. It's particularly useful when working with functions that return multiple values or when dealing with large data structures like API responses.

Understanding destructuring can significantly improve the maintainability and readability of your code, making it easier for you and others to understand and work with.

Core Content

Destructuring Arrays

Arrays are ordered collections of values. With destructuring, you can easily extract these values into individual variables.

Basic Array Destructuring

Let's start with a simple example:

basicArrayDestructuring.js
1const [first, second, third] = [10, 20, 30];
2console.log(first); // Output: 10
3console.log(second); // Output: 20
4console.log(third); // Output: 30
Output
10
20
30

In this example, the values 10, 20, and 30 are destructured into the variables first, second, and third.

Skipping Values

If you want to skip certain values in an array, you can use commas to leave them out:

skipValues.js
1const [first, , third] = [10, 20, 30];
2console.log(first); // Output: 10
3console.log(third); // Output: 30
Output
10
30

Here, the value 20 is skipped.

Using Rest Syntax

The rest syntax (...) can be used to capture the remaining elements of an array into a new array:

restSyntax.js
1const [first, ...rest] = [10, 20, 30, 40];
2console.log(first); // Output: 10
3console.log(rest); // Output: [20, 30, 40]
Output
10
[20, 30, 40]

In this example, first gets the first element, and rest captures the remaining elements into a new array.

Destructuring Objects

Objects are collections of key-value pairs. Destructuring allows you to extract these values into variables using their keys.

Basic Object Destructuring

Here's a basic example:

basicObjectDestructuring.js
1const { name, age } = { name: 'Alice', age: 25 };
2console.log(name); // Output: Alice
3console.log(age); // Output: 25
Output
Alice
25

In this example, the values name and age are destructured from the object into variables with the same names.

Renaming Variables

You can rename variables during destructuring using the colon (:) syntax:

renameVariables.js
1const { name: fullName, age } = { name: 'Alice', age: 25 };
2console.log(fullName); // Output: Alice
3console.log(age); // Output: 25
Output
Alice
25

Here, the name property is renamed to fullName.

Providing Default Values

You can also provide default values for variables in case the property doesn't exist in the object:

defaultValues.js
1const { name = 'Guest', age } = { age: 25 };
2console.log(name); // Output: Guest
3console.log(age); // Output: 25
Output
Guest
25

In this example, if the name property doesn't exist in the object, it defaults to 'Guest'.

Nested Destructuring

Destructuring can also be applied to nested arrays and objects.

Nested Array Destructuring

Here's an example with nested arrays:

nestedArrayDestructuring.js
1const [first, [second, third]] = [10, [20, 30]];
2console.log(first); // Output: 10
3console.log(second); // Output: 20
4console.log(third); // Output: 30
Output
10
20
30

In this example, the nested array [20, 30] is destructured into second and third.

Nested Object Destructuring

Here's an example with nested objects:

nestedObjectDestructuring.js
1const { user: { name, age } } = { user: { name: 'Alice', age: 25 } };
2console.log(name); // Output: Alice
3console.log(age); // Output: 25
Output
Alice
25

In this example, the nested object { name: 'Alice', age: 25 } is destructured into name and age.

Practical Example

Let's create a practical example where we use destructuring to handle user data from an API response.

practicalExample.js
1const userData = {
2id: 1,
3name: 'Alice',
4age: 25,
5address: {
6 city: 'New York',
7 zip: '10001'
8}
9};
10
11// Destructuring the user data
12const { id, name, age, address: { city, zip } } = userData;
13
14console.log(`User ID: ${id}`);
15console.log(`Name: ${name}`);
16console.log(`Age: ${age}`);
17console.log(`City: ${city}`);
18console.log(`Zip Code: ${zip}`);
Output
User ID: 1
Name: Alice
Age: 25
City: New York
Zip Code: 10001

In this example, we destructured the userData object to extract individual properties and nested properties into variables. This makes it easier to work with the data in our application.

Summary

ConceptDescription
Array DestructuringExtract values from arrays into distinct variables.
Object DestructuringExtract properties from objects into distinct variables.
Renaming VariablesRename variables during destructuring using the colon syntax (:).
Default ValuesProvide default values for variables in case the property doesn't exist.
Nested DestructuringApply destructuring to nested arrays and objects.

What's Next?

In the next lesson, we'll explore the Spread & Rest Operators in JavaScript. These operators allow you to work with arrays and objects in more flexible ways, enabling you to easily copy, merge, or manipulate them. Stay tuned!

If you have any questions or need further clarification on destructuring, feel free to ask in the comments below.


PreviousJavaScript Template LiteralsNext JavaScript Spread & Rest Operators

Recommended Gear

JavaScript Template LiteralsJavaScript Spread & Rest Operators