JavaScript/C2/Loops-in-JS/English
Title of the script:Loops in JS
Author: Jayesh Katta Ramalingaiah
Domain Reviewer:
Novice Reviewer:
Keywords: JavaScript, HTML, for, while, do.. While, for..in
|
|
Slide: Title | Hello and Welcome to the spoken tutorial on “Loops 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: Types of Loops in JS | The different types of loops are:
Let us take an example and understand this better. |
Switch to the Visual Studio Code editor. | Switch to the Visual Studio Code editor. |
[Editor]
Open Folder -> Practice-JS |
In the Welcome page, click on the Open folder link at the top left corner.
|
[Editor] Click on
|
In the Explorer panel on the left, under Practice-JS click on the file named index.html
|
[Editor] Type:
<html lang="en"> <head> <title>Loops</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. |
Press: Alt + L and Alt + O
|
Now press the key combinations Alt + L and Alt + O.
|
Show firefox | The default browser will open automatically and a new tab opens. |
[Firefox] Press Ctrl + Shift + I
|
Now press Ctrl + Shift + I keys together.
|
[Firefox] Click on the Console tab | Now click on the console tab in the developer tools. |
Switch to Editor | Switch back to the editor. |
[Editor] Click on
|
In the Explorer panel on the left, under Practice-JS click on the file named main.js |
[Editor] Type:
console.log(i); } |
In the main.js file, replace the code as shown.
|
[Editor] Highlight:
|
Let me explain how the for loop works in JS.
|
[Editor] Highlight:
|
Next, the condition is checked for the value of i = 0.
|
[Editor] Highlight:
|
So, the block of code within the for loop gets executed.
|
[Editor] Highlight:
|
After this we increment i by 1 using i++, which is basically i = i +1.
|
Press: Ctrl + S | Save the file. |
Switch to Browser | Switch back to the browser. |
[Browser] [Console Tab]:
Highlight:
1 2 3 4 |
We see the output as 0 1 2 3 4.
|
Switch to Editor | Switch back to the editor.
|
[Editor] Type:
while (n < 5) { console.log(n); n++; } |
In the main.js file, replace the code as shown. |
[Editor] Highlight:
|
Let me explain how the while loop works.
|
[Editor] Highlight:
|
Then comes the while loop.
|
[Editor] Highlight:
|
Here, the block of code gets executed and logs the value. |
[Editor] Highlight:
|
Here, inside the block we have written n++
|
Press: Ctrl + S | Save the file. |
Switch to browser | Switch back to the browser. |
[Browser] [Console Tab]:
Highlight:
1 2 3 4 |
Now, we see the output as 0 1 2 3 4.
|
Switch to editor | Switch back to the editor.
|
[Editor] Type:
do { console.log(m); m++; } while (m < 5); |
In the main.js file, replace the code as shown. |
[Editor] Highlight:
var m = 0; |
The variable declaration and initialisation happens outside the loop.
We have initialised the variable m to zero. |
Only Narration | This loop is also called an exit controlled loop because the condition check will happen at the end. |
[Editor] Highlight:
|
In do.. while loop, the condition is always checked while exiting the block.
|
[Editor] Highlight:
m++; |
In the loop, we are just logging the value and then increasing it by 1.
|
[Editor] Highlight:
|
This is where the condition check happens.
|
Press: Ctrl + S | Save the file. |
Switch to browser | Switch back to the browser. |
[Browser] [Console Tab]:
Highlight:
1 2 3 4 |
Now, we see the output as 0 1 2 3 4.
|
Slide: Comparison between loops | To conclude, if the condition is not satisfied-
|
Slide: Iteration over an array | All these three loops can iterate over an array taking the index value as counter. |
Only Narration | Next, we shall learn how to loop over properties of an object.
|
Switch to Editor | Switch back to the editor. |
[Editor] Type:
name1: "Spoken", name2: "Tutorials", };
console.log(obj[key]); } |
In the main.js file, replace the code as shown. |
[Editor] Highlight:
name1: "Spoken", name2: "Tutorials", }; |
Here, we are initializing an object to loop over and print the values in all the object properties. |
[Editor] Highlight:
|
In this loop, we need to create a temporary variable, which holds the property in each and every iteration.
|
[Editor] Highlight:
|
Here, we are logging the value as obj[key]
|
Press: Ctrl + S | Save the file. |
Switch to browser | Switch back to the browser. |
[Browser] [Console Tab]:
Highlight:
Tutorials |
Here, we see in the output that the values for all the properties in the object are printed.
|
Only narration | With this we have come to the end of this tutorial.
|
Slide: Summary | In this tutorial, we have learnt about:
|
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 Ministry of Education (MoE), Government of India |
Slide: Thanks | The script for this tutorial is contributed by Jayesh.
And this is Praveen from IIT Bombay signing off. Thank you for joining. |