JavaScript/C4/Object-and-Array-Destructuring-in-JS/English
Title of the script: Object and Array Destructuring
Author: Jayesh Katta Ramalingaiah
Domain Reviewer: Vigneshwer Dhinakaran
Novice Reviewer: Praveeen S.
Keywords: JavaScript, HTML, abject, array, shorthands, spread, template literals
|
|
Slide: Title | Hello and welcome to the spoken tutorial on “Object and Array Destructuring in JS”. |
Slide:
Learning Objectives |
In this tutorial, we will learn about:
|
Slide: System Specifications | This tutorial is recorded using:
However, you may use any other browser of your choice. |
Slide: Pre-requisites | To practice this tutorial,
|
Slide: Code files |
|
Slide: Destructuring |
|
Only Narration | Now, let us take an example and understand Array and Object Destructuring better. |
Show VS editor | Open Visual Studio Code editor |
[Editor] Welcome Page -> Open Folder -> Practice-JS -> index.html & main.js
|
Then open the files index.html and main.js as explained in the earlier tutorials.
|
[Editor] Type:
<html lang="en"> <head> <title>Object and Array Destructuring & Short Hands</title> </head> <body> <script src="main.js"></script> </body> </html> |
In the index.html file, replace the code as shown.
|
Press: Ctrl + S
|
Save the file and start the Live server. |
[Firefox] Press Ctrl + Shift + I
|
In the web browser, open the Browser developer tools panel and go to the Console tab. |
Switch to Editor | Switch back to the editor. |
[Editor] Type:
const [name, age] = arr;
console.log(age); |
In the main.js file, replace the code as shown. |
[Editor] Highlight:
const arr = ["Jayesh", 20]; |
Here, I have created an array with the variable name arr.
|
[Editor] Highlight:
|
Instead of assigning the array index separately for the variables name and age.
|
[Editor] Highlight:
console.log(age); |
To verify this, I’m logging the name and age variables on the console. |
Press: Ctrl + S | Save the file. |
Switch to Browser | Switch back to the browser. |
[Browser] [Console Tab]:
Highlight:
20 |
The output verifies that the array values are assigned to the name and age variables.
|
Only narration | Next, let's learn how to destructure an object. |
Switch to Editor | Switch back to the editor. |
[Editor] Type:
const { name, age } = obj;
console.log(age); |
In the main.js file, replace the code as shown. |
[Editor] Highlight:
|
Here, I have declared an object with the variable name obj.
|
[Editor] Highlight:
|
This is similar to the array example which we have seen before.
|
[Editor] Highlight:
console.log(age); |
To verify this, I’m logging name and age variables separately on the console. |
Press: Ctrl + S | Save the file. |
Switch to Browser | Switch back to the browser. |
[Browser] [Console Tab]:
Highlight:
20 |
The output verifies that the object is destructured.
|
Only Narration | Here, we have 2 properties.
And we have passed 2 variable names in both of our array and object examples.
|
Slide: Object Destructing | For Objects, the variable name should be the same as the object property name.
|
Slide: Spread Operator | The syntax of Spread Operator is very simple.
|
Only Narration | Now, let us take an example and understand this better. |
Switch to Editor | Switch back to the editor. |
[Editor] Type:
name: "Jayesh", age: 20, location: "Bangalore", }; const { name, ...details } = obj;
console.log(details); |
In the main.js file, replace the code as shown. |
[Editor] Highlight:
name: "Jayesh", age: 20, location: "Bangalore", }; |
Here, I have declared an object with 3 properties as per our discussed scenario. |
[Editor] Highlight:
|
To assign the name property value in the object to the name variable, I’m declaring the name here. |
[Editor] Highlight:
...details |
And, to store the rest of the two properties in the details variable, I’m using a spread operator.
|
[Editor] Highlight:
console.log(details); |
To verify, I’m logging both the variables separately. |
Press: Ctrl + S | Save the file. |
Switch to Browser | Switch back to the browser. |
[Browser] [Console Tab]:
Highlight:
|
Here is the verification for the newly created name variable.
|
[Browser] [Console Tab]:
Highlight:
|
And the rest of the properties in objects are assigned to details. |
Only Narration | Next, let’s learn about Template literals which is a better way to concatenate strings. |
Slide: Template Literals |
|
Only Narration | Now, let us take an example and understand this better. |
Switch to Editor | Switch back to the editor. |
[Editor] Type:
console.log(`${name} lives in ${city}`); }
|
In the main.js file, replace the code as shown. |
[Editor] Highlight:
console.log(`${name} lives in ${city}`); } |
Here, I have declared a function named display, which accepts an object as a parameter. |
[Editor] Highlight:
|
The passed object should have the name and location as the properties. |
[Editor] Highlight:
|
Here, I have a requirement to access the location property as city variable.
|
[Editor] Highlight:
|
To verify this, I have logged a string that accepts name and city variables. |
[Editor] Highlight:
|
As mentioned, I’m writing my string using backtick here to concatenate variables and strings.
|
[Editor] Highlight:
|
Here, I’m not using location anymore, as the location is cast as city. |
[Editor] Highlight:
|
Here, I have made a function call by passing an object having a name and location as parameters. |
Press: Ctrl + S
Switch to browser |
Save the file and Switch back to the browser. |
[Browser] [Console Tab]:
Highlight:
|
The console output verifies that the string has concatenated properly using Template literals.
|
Only Narration | With this, we have come to the end of this tutorial.
|
Slide: Summary | In this tutorial, we have learnt:
|
Slide: Assignment | As an assignment:
|
Slide: About Spoken Tutorial Project |
|
Slide: Spoken tutorial workshops |
|
Slide: Forum questions | Pls post your timed queries in this forum. |
Slide: Acknowledgement | Spoken Tutorial Project is funded by the Ministry of Education (MoE), Government of India. |
Slide: Thanks | The script for this tutorial is contributed by Jayesh and this is Praveen signing off. Thank you for joining |