- Javascript Basics Tutorial
- Javascript - Home
- JavaScript - Overview
- JavaScript - Features
- JavaScript - Enabling
- JavaScript - Placement
- JavaScript - Syntax
- JavaScript - Hello World
- JavaScript - Console.log()
- JavaScript - Comments
- JavaScript - Variables
- JavaScript - let Statement
- JavaScript - Constants
- JavaScript - Data Types
- JavaScript - Type Conversions
- JavaScript - Strict Mode
- JavaScript - Reserved Keywords
- JavaScript Operators
- JavaScript - Operators
- JavaScript - Arithmetic Operators
- JavaScript - Comparison Operators
- JavaScript - Logical Operators
- JavaScript - Bitwise Operators
- JavaScript - Assignment Operators
- JavaScript - Conditional Operators
- JavaScript - typeof Operator
- JavaScript - Nullish Coalescing Operator
- JavaScript - Delete Operator
- JavaScript - Comma Operator
- JavaScript - Grouping Operator
- JavaScript - Yield Operator
- JavaScript - Spread Operator
- JavaScript - Exponentiation Operator
- JavaScript - Operator Precedence
- JavaScript Control Flow
- JavaScript - If...Else
- JavaScript - While Loop
- JavaScript - For Loop
- JavaScript - For...in
- Javascript - For...of
- JavaScript - Loop Control
- JavaScript - Break Statement
- JavaScript - Continue Statement
- JavaScript - Switch Case
- JavaScript - User Defined Iterators
- JavaScript Functions
- JavaScript - Functions
- JavaScript - Function Expressions
- JavaScript - Function Parameters
- JavaScript - Default Parameters
- JavaScript - Function() Constructor
- JavaScript - Function Hoisting
- JavaScript - Self-Invoking Functions
- JavaScript - Arrow Functions
- JavaScript - Function Invocation
- JavaScript - Function call()
- JavaScript - Function apply()
- JavaScript - Function bind()
- JavaScript - Closures
- JavaScript - Variable Scope
- JavaScript - Global Variables
- JavaScript - Smart Function Parameters
- JavaScript Objects
- JavaScript - Number
- JavaScript - Boolean
- JavaScript - Strings
- JavaScript - Arrays
- JavaScript - Date
- JavaScript - DataView
- JavaScript - Math
- JavaScript - RegExp
- JavaScript - Symbol
- JavaScript - Sets
- JavaScript - WeakSet
- JavaScript - Maps
- JavaScript - WeakMap
- JavaScript - Iterables
- JavaScript - Reflect
- JavaScript - TypedArray
- JavaScript - Template Literals
- JavaScript - Tagged Templates
- Object Oriented JavaScript
- JavaScript - Objects
- JavaScript - Classes
- JavaScript - Object Properties
- JavaScript - Object Methods
- JavaScript - Static Methods
- JavaScript - Display Objects
- JavaScript - Object Accessors
- JavaScript - Object Constructors
- JavaScript - Native Prototypes
- JavaScript - ES5 Object Methods
- JavaScript - Encapsulation
- JavaScript - Inheritance
- JavaScript - Abstraction
- JavaScript - Polymorphism
- JavaScript - Destructuring Assignment
- JavaScript - Object Destructuring
- JavaScript - Array Destructuring
- JavaScript - Nested Destructuring
- JavaScript - Optional Chaining
- JavaScript - Global Object
- JavaScript - Mixins
- JavaScript - Proxies
- JavaScript Versions
- JavaScript - History
- JavaScript - Versions
- JavaScript - ES5
- JavaScript - ES6
- ECMAScript 2016
- ECMAScript 2017
- ECMAScript 2018
- ECMAScript 2019
- ECMAScript 2020
- ECMAScript 2021
- ECMAScript 2022
- JavaScript Cookies
- JavaScript - Cookies
- JavaScript - Cookie Attributes
- JavaScript - Deleting Cookies
- JavaScript Browser BOM
- JavaScript - Browser Object Model
- JavaScript - Window Object
- JavaScript - Document Object
- JavaScript - Screen Object
- JavaScript - History Object
- JavaScript - Navigator Object
- JavaScript - Location Object
- JavaScript - Console Object
- JavaScript Web APIs
- JavaScript - Web API
- JavaScript - History API
- JavaScript - Storage API
- JavaScript - Forms API
- JavaScript - Worker API
- JavaScript - Fetch API
- JavaScript - Geolocation API
- JavaScript Events
- JavaScript - Events
- JavaScript - DOM Events
- JavaScript - addEventListener()
- JavaScript - Mouse Events
- JavaScript - Keyboard Events
- JavaScript - Form Events
- JavaScript - Window/Document Events
- JavaScript - Event Delegation
- JavaScript - Event Bubbling
- JavaScript - Event Capturing
- JavaScript - Custom Events
- JavaScript Error Handling
- JavaScript - Error Handling
- JavaScript - try...catch
- JavaScript - Debugging
- JavaScript - Custom Errors
- JavaScript - Extending Errors
- JavaScript Important Keywords
- JavaScript - this Keyword
- JavaScript - void Keyword
- JavaScript - new Keyword
- JavaScript - var Keyword
- JavaScript HTML DOM
- JavaScript - HTML DOM
- JavaScript - DOM Methods
- JavaScript - DOM Document
- JavaScript - DOM Elements
- JavaScript - DOM Forms
- JavaScript - Changing HTML
- JavaScript - Changing CSS
- JavaScript - DOM Animation
- JavaScript - DOM Navigation
- JavaScript - DOM Collections
- JavaScript - DOM Node Lists
- JavaScript Miscellaneous
- JavaScript - Ajax
- JavaScript - Async Iteration
- JavaScript - Atomics Objects
- JavaScript - Rest Parameter
- JavaScript - Page Redirect
- JavaScript - Dialog Boxes
- JavaScript - Page Printing
- JavaScript - Validations
- JavaScript - Animation
- JavaScript - Multimedia
- JavaScript - Image Map
- JavaScript - Browsers
- JavaScript - JSON
- JavaScript - Multiline Strings
- JavaScript - Date Formats
- JavaScript - Get Date Methods
- JavaScript - Set Date Methods
- JavaScript - Modules
- JavaScript - Dynamic Imports
- JavaScript - BigInt
- JavaScript - Blob
- JavaScript - Unicode
- JavaScript - Shallow Copy
- JavaScript - Call Stack
- JavaScript - Reference Type
- JavaScript - IndexedDB
- JavaScript - Clickjacking Attack
- JavaScript - Currying
- JavaScript - Graphics
- JavaScript - Canvas
- JavaScript - Debouncing
- JavaScript - Performance
- JavaScript - Style Guide
- JavaScript Useful Resources
- JavaScript - Questions And Answers
- JavaScript - Quick Guide
- JavaScript - Functions
- JavaScript - Resources
JavaScript - Nested Destructuring
Nested Destructuring
The Nested Destructuring in JavaScript allows us to extract data from nested objects and arrays. An object (or array) can contain another object (or array) inside itself, known as a nested object (or array). Unpacking the nested objects or arrays is called nested destructuring. We can extract all or some data from the objects or arrays using destructuring.
We can assign the extracted data from nested array or object to the variables. This is referred as nested destructuring assignment. When using nested destructuring to get some values from a nested array or object, you have to follow the structure of the array or object.
Nested Object Destructuring
This section will demonstrate nested object destructuring in JavaScript.
Syntax
The syntax of nested object destruction in JavaScript is as follows −
const {prop1, prop2: {nestedprop1, nestedprop2}} = obj;
In the above syntax, prop1 is a property of the object, and the prop2 property contains the nested object, having nestedprop1 and nestedprop2 properties.
Example
In the example below, the car object contains the brand, model, and info properties. The info property contains the nested object containing the price and color properties.
We have destructured the nested object and printed the values of the variables in the output.
<html> <body> <p id = "output"> </p> <script> const car = { brand: "Hyundai", model: "Verna", info: { price: 1200000, // Nested properties color: "white", } } const { brand, model, info: { price, color } } = car; // Destructuring document.getElementById("output").innerHTML = `Brand: ${brand} <br> Model: ${model} <br> Price: ${price} <br> Color: ${color}`; </script> </body> </html>
Output
Brand: Hyundai Model: Verna Price: 1200000 Color: white
Example: Nested object destructuring and renaming variables
The below code demonstrates that you can rename the variables while unpacking the nested object properties.
We have renamed the brand, model, price, and color variables to company, name, cost, and carColor.
<html> <body> <p id = "output"> </p> <script> const car = { brand: "Hyundai", model: "Verna", info: { price: 1200000, // Nested properties color: "white", } } // Destructuring const {brand: company, model: name, info: {price: cost, color: carColor }} = car; document.getElementById("output").innerHTML = `Company: ${company}, Name: ${name}, Cost: ${cost}, Color: ${carColor}`; </script> </body> </html>
Output
Company: Hyundai, Name: Verna, Cost: 1200000, Color: white
Example: Nested object destructuring and default values
You can use the assignment operator to assign the default values to the variables. Whenever particular property of the object is undefined, it initializes the variable with the default value.
Here, we have renamed each variable and assigned default values. The 'science' property is not defined in the grades (nested object) object. So, the code prints its default value in the output.
<html> <body> <p id = "output"> </p> <script> const student = { firstName: "Sam", lastName: "Raina", grades: { English: 75, } }; const { firstName: name = "Jay", lastName: surname = "Shah", grades: { English: eng = 0, Science: sci = 0 } } = student; document.getElementById("output").innerHTML = `Name: ${name} <br> Surname: ${surname} <br> English: ${eng} <br> Science: ${sci}`; </script> </body> </html>
Output
Name: Sam Surname: Raina English: 75 Science: 0
Example: Nested object destructuring and rest operator
The rest operator allows you to collect the remaining properties into a single object.
In the below code, the grades object contains 4 different properties. We have stored the value of the Maths property in the Maths variables and other properties in the 'allGrades' variable using the rest operator. The 'allGrades' is an object containing 3 properties.
<html> <body> <p id = "output"> </p> <script> const student = { firstName: "Kunal", lastName: "Karma", grades: { English: 75, Maths: 87, SocialScience: 90, Science: 80, } }; const { firstName, lastName, grades: { Maths, ...allGrades } } = student; document.getElementById("output").innerHTML = `firstName: ${firstName} <br> lastName: ${lastName} <br> Maths: ${Maths} <br> allGrades: ${JSON.stringify(allGrades)} <br> `; </script> </body> </html>
Output
firstName: Kunal lastName: Karma Maths: 87 allGrades: {"English":75,"SocialScience":90,"Science":80}
Nested Array Destructuring
This section will demonstrate nested array destructuring in JavaScript.
Syntax
The syntax to unpack nested array elements (nested array destructuring) in JavaScript is as follows −
const [a, [b, c], d] = arr;
In the above syntax, we store the nested array elements in the b and c variables.
Example
In the below code, the arr array contains the nested array. We unpack the nested array elements into the variables b and c. In the output, you can observe the values of the b and c variables.
<html> <body> <p id = "output"> </p> <script> const arr = [10, [15, 20], 30]; const [a, [b, c], d] = arr; document.getElementById("output").innerHTML = "a = " + a + ", b = " + b + ", c = " + c + ", d = " + d; </script> </body> </html>
Output
a = 10, b = 15, c = 20, d = 30
Example: Skipping elements of the nested array
Assignment destructuring allows you to skip the elements of the nested array. Here, the arr array contains two nested arrays. We skip the first element of each nested array while destructuring the nested array.
<html> <body> <p id = "output"> </p> <script> const arr = [2, [3, 4], [9, 10]]; const [a, [, b], [, c]] = arr; document.getElementById("output").innerHTML = "a = " + a + ", b = " + b + ", c = " + c; </script> </body> </html>
Output
a = 2, b = 4, c = 10
Example: Nested array destructuring and default values
You can assign default values to the variables like objects while destructuring the nested arrays.
Here, the first nested array of arr [3, 4] contains two elements. While destructuring, we skipped the first two elements and used the variable p to get the third element, but the nested array contains only two elements. So, the value of the variable p is a 29 default value.
<html> <body> <p id = "output"> </p> <script> const arr = [2, [3, 4], [9, 10]]; const [, [, , p = 29], [, q]] = arr; document.getElementById("output").innerHTML = "p = " + p + ", q = " + q; </script> </body> </html>
Output
p = 29, q = 10
Example: Nested array destructuring and rest operator
You can use the rest operator with nested array destructuring. Here, variable b stores the remaining elements of the first nested array, and variable c stores the remaining elements of the parent array, which includes the second nested array and last array element.
<html> <body> <p id = "output"> </p> <script> const arr = [[6, 7, 8, 9], [10, 11, 12, 13], 14]; const [[a, ...b], ...c] = arr document.getElementById("output").innerHTML = "a = " + a + "<br> b = " + b + "<br> c = " + c; </script> </body> </html>
Output
a = 6 b = 7,8,9 c = 10,11,12,13,14
Array Within Object – Nested Destructuring
Sometimes, we need to destructure the object containing the array. Let's understand it via the example below.
Example
In the example below, the num2 property of the numbers object contains the array. We destructure the object properties and print the values in the output.
<html> <body> <p id = "output"> </p> <script> const numbers = { num1: 10, num2: [40, 6, 5], } const {num1, num2: [a, b, c]} = numbers; document.getElementById("output").innerHTML = "num1 = " + num1 + ", a = " + a + ", b = " + b + ", c = " + c; </script> </body> </html>
Output
num1 = 10, a = 40, b = 6, c = 5
Object Within Array – Nested Destructuring
In some cases, an array can also contain the object, and you need to destructure the object from the array.
Example
In the below code, the numbers array contains the object containing the p and q properties.
After that, we destructure the array and unpack the object properties.
<html> <body> <p id = "output"> </p> <script> const numbers = [10, { p: 20, q: 30 }] const [a, { p, q }] = numbers; document.getElementById("output").innerHTML = "a = " + a + ", p = " + p + ", q = " + q; </script> </body> </html>
Output
a = 10, p = 20, q = 30