Difference between revisions of "JavaScript/C4/Object-and-Array-Destructuring-in-JS/English"
(Created page with "Title of the script: Object and Array Destructuring Author: Jayesh Katta Ramalingaiah Domain Reviewer: Novice Reviewer: Keywords: JavaScript, HTML, abject, array, shorth...") |
Nancyvarkey (Talk | contribs) |
||
Line 8: | Line 8: | ||
Keywords: JavaScript, HTML, abject, array, shorthands, spread, template literals | Keywords: JavaScript, HTML, abject, array, shorthands, spread, template literals | ||
− | |||
− | |||
{| border = “1” | {| border = “1” | ||
Line 24: | Line 22: | ||
Learning Objectives | Learning Objectives | ||
|| In this tutorial, we will learn about: | || In this tutorial, we will learn about: | ||
− | |||
− | |||
* '''Spread Operator''' | * '''Spread Operator''' | ||
* '''Template Literals''' | * '''Template Literals''' | ||
* '''Objects and''' | * '''Objects and''' | ||
* '''Array Destructuring''' | * '''Array Destructuring''' | ||
− | |||
− | |||
|- | |- | ||
|| Slide: System Specifications | || Slide: System Specifications | ||
|| This tutorial is recorded using: | || This tutorial is recorded using: | ||
− | |||
− | |||
* '''Ubuntu Linux''' OS version 18.04 | * '''Ubuntu Linux''' OS version 18.04 | ||
* '''Visual Studio Code''' version 1.45.0 ('''code editor''') | * '''Visual Studio Code''' version 1.45.0 ('''code editor''') | ||
Line 47: | Line 39: | ||
|| Slide: Pre-requisites | || Slide: Pre-requisites | ||
|| To practice this tutorial, | || To practice this tutorial, | ||
− | + | * You should be familiar with writing and executing''' JS''' files. | |
− | + | ||
− | * You should be familiar with writing and executing''' JS''' files | + | |
* If not, please go through the prerequisite tutorials on this website. | * If not, please go through the prerequisite tutorials on this website. | ||
− | |||
− | |||
|- | |- | ||
Line 60: | Line 48: | ||
* Pls download and extract the files. | * Pls download and extract the files. | ||
* Make a copy and then use them for practising. | * Make a copy and then use them for practising. | ||
− | |||
− | |||
|- | |- | ||
|| Slide: Destructuring | || Slide: Destructuring | ||
|| | || | ||
− | * '''Destructuring '''is | + | * '''Destructuring '''is an''' assignment syntax '''in '''JavaScript''' |
* It makes it possible to unpack values defined in '''Object '''or '''Array'''. | * It makes it possible to unpack values defined in '''Object '''or '''Array'''. | ||
− | |||
− | |||
|- | |- | ||
Line 123: | Line 107: | ||
Press: Alt + L and Alt + O | Press: Alt + L and Alt + O | ||
− | || Save the file and | + | || Save the file and start the '''Live server'''. |
|- | |- | ||
Line 129: | Line 113: | ||
− | Point to the | + | Point to the '''browser developer tools''' |
Click on the '''Console''' tab | Click on the '''Console''' tab | ||
− | || In the web browser, open the '''Browser developer tools''' panel and go to the ''' | + | || In the '''web browser''', open the '''Browser developer tools''' panel and go to the '''Console tab'''. |
|- | |- | ||
Line 317: | Line 301: | ||
− | + | ('''…) '''three dots followed by the '''variable '''name. | |
|- | |- | ||
Line 429: | Line 413: | ||
|| | || | ||
* '''Template literals''' are written using open and closed '''backticks'''. | * '''Template literals''' are written using open and closed '''backticks'''. | ||
− | * | + | * Here is an example. |
* '''Variables '''values can be displayed using''' ${} ('''the '''dollar''' symbol and curly braces''')''' in a '''string'''. | * '''Variables '''values can be displayed using''' ${} ('''the '''dollar''' symbol and curly braces''')''' in a '''string'''. | ||
− | |||
− | |||
|- | |- | ||
Line 479: | Line 461: | ||
location: city | location: city | ||
− | || Here, | + | || Here, I have a requirement to access the '''location property '''as '''city variable.''' |
Line 502: | Line 484: | ||
− | And the '''variable '''names are wrapped up with '''$ | + | And the '''variable '''names are wrapped up with '''$ dollar''' symbol and curly braces'''{}.''' |
|- | |- | ||
Line 509: | Line 491: | ||
${city} | ${city} | ||
− | || Here, I’m not using '''location '''anymore, as the '''location''' is cast as | + | || Here, I’m not using '''location '''anymore, as the '''location''' is cast as '''city.''' |
|- | |- | ||
Line 523: | Line 505: | ||
|- | |- | ||
− | || Switch to ''' | + | || Switch to '''browser''' |
|| Switch back to the '''browser.''' | || Switch back to the '''browser.''' | ||
Line 548: | Line 530: | ||
|| Slide''': '''Summary''' ''' | || Slide''': '''Summary''' ''' | ||
|| In this tutorial, we have learnt: | || In this tutorial, we have learnt: | ||
− | |||
− | |||
* '''Spread Operator''' | * '''Spread Operator''' | ||
* '''Template Literals''' | * '''Template Literals''' | ||
* '''Object '''and | * '''Object '''and | ||
* '''Array Destructuring''' | * '''Array Destructuring''' | ||
− | |||
− | |||
|- | |- | ||
|| Slide: Assignment | || Slide: Assignment | ||
|| As an assignment: | || As an assignment: | ||
− | |||
− | |||
* Open the file '''assignment.js''' which you have created earlier. | * Open the file '''assignment.js''' which you have created earlier. | ||
* Clear the existing code. | * Clear the existing code. | ||
− | * '''Declare | + | * '''Declare variable marks''' |
* Assign an '''array '''to''' variable marks '''containing''' 88, 90''' and '''92''' | * Assign an '''array '''to''' variable marks '''containing''' 88, 90''' and '''92''' | ||
* '''Destruct''' the '''array '''and assign the values to '''m1''', '''m2 '''and''' m3''' respectively | * '''Destruct''' the '''array '''and assign the values to '''m1''', '''m2 '''and''' m3''' respectively | ||
Line 570: | Line 546: | ||
* Open the file '''MyPage.html''' in a '''web browser''' | * Open the file '''MyPage.html''' in a '''web browser''' | ||
* Observe the output in the '''browser’s console''' | * Observe the output in the '''browser’s console''' | ||
− | |||
− | |||
|- | |- | ||
Line 577: | Line 551: | ||
|| | || | ||
* The video at the following link summarises the Spoken Tutorial project. | * The video at the following link summarises the Spoken Tutorial project. | ||
− | * Please download and watch it | + | * Please download and watch it. |
− | + | ||
− | + | ||
|- | |- | ||
Line 586: | Line 558: | ||
* We conduct workshops using spoken tutorials and give certificates. | * We conduct workshops using spoken tutorials and give certificates. | ||
* For more details, please write to us. | * For more details, please write to us. | ||
− | |||
− | |||
|- | |- | ||
|| Slide: Forum questions | || Slide: Forum questions | ||
− | || Pls post your timed queries in this forum | + | || Pls post your timed queries in this forum. |
|- | |- | ||
|| Slide: Acknowledgement | || Slide: Acknowledgement | ||
− | || Spoken Tutorial Project is funded by the Ministry of Education (MoE), Government of India | + | || Spoken Tutorial Project is funded by the Ministry of Education (MoE), Government of India. |
|- | |- |
Revision as of 23:41, 27 May 2021
Title of the script: Object and Array Destructuring
Author: Jayesh Katta Ramalingaiah
Domain Reviewer:
Novice Reviewer:
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 | Note:
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 | Save the file. |
Switch to browser | 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 |