In this JavaScript For Loop tutorial, we do check the complete working of for loop with the help of different examples. So we do start the complete tutorial step by step,
- Introduction of JavaScript For Loop
- Workflow of JavaScript For Loop
- Different types of For loop with example
- Conclusion
Introduction of JavaScript For Loop
Loop through a block of code a number of times that is defined by users. This means the For loop provides three optional expressions where we define the conditions to run the block of code.
Workflow of JavaScript For Loop
Syntax of JavaScript For Loop
1 2 3 |
for (initialization; condition; post-expression) { // Statement(s) to be executed if condition is true } |
Initialization Here user initialize the value of the loop from where loop starts and it executed one time on the complete loop process.
Condition Here we define the condition when the loop will stop. Means this executes at every iteration and check the condition whether it is true or false. If the defined condition is true then loop iteration will continue, if false then the loop will be terminated.
Condition part of for loop is optional, if you did not define, it always takes true
.
post-expression when the loop executes it will help to update the counter variable and the loop will continue.
Example of JS For Loop
1 2 3 4 5 6 7 8 9 10 |
<p id="result"></p> <script> var text = ""; var i; for (i = 0; i < 5; i++) { text += "Count " + i + "<br>"; } document.getElementById("result").innerHTML = text; </script> |
Output:
Count 0
Count 1
Count 2
Count 3
Count 4
Code Explanations:
- In a first statement, we set the variable (var i = 0) before the loop starts.
- Define the condition when the loop will stop (when the value
i
must be greater than 5). - Then loop will increase the value of
i
on each execution till the condition will false.
Types of For Loop With Example
Here are 3 major type of for loop or you can say different kind of loops in JS.
- For: Used to loop through the code n number of times till the condition is false
- For/in: Used to loop through object properties
- For/of: Used to loop through an array of iterable objects
The For/In Loop
The For/In
loop used to loop through the properties of an object.
Syntax of The For/In Loop
1 2 3 |
for (key in object) { // code block to be executed } |
Example of The For/In Loop
1 2 3 4 5 6 7 8 9 10 11 |
<p id="result"></p> <script> var details = ""; var person = {name:"John", design:"Developer", age:25}; var x; for (x in person) { details += person[x] + " "; } document.getElementById("result").innerHTML = details; </script> |
Output:
John Developer 25
Code Explained:
- Here we create an object named
person
- And the For/In loop iterates the person object.
- Each iteration returns a key 0,1,2.. etc
(x)
- The key is used to access the data of person object
(person[x])
The For/In Loop Over Arrays
1 2 3 |
for (variable in array) { code } |
Example of The For/In Loop Over Arrays
1 2 3 4 5 6 7 8 9 10 11 |
<p id="result"></p> <script> var text = ""; var words = ['bikash', 'John', 'Sundar']; var x; for (x in words) { text += words[x] + "<br>"; } document.getElementById("result").innerHTML = text; </script> |
Output:
bikash
John
Sundar
The For/Of Loop
The For/Of Loop is used to loop through the values of an iterable object.
1 2 3 |
for (variable of iterable) { // code block to be executed } |
Example of The For/Of Loop
1 2 3 4 5 6 7 8 9 10 11 12 |
<p id="result"></p> <script> let bikes = ["BMW", "SUZUKI", "ROYAL"]; let text = ""; for (let x of bikes) { text += x + "<br>"; } document.getElementById("result").innerHTML = text; </script> |
Output:
BMW
SUZUKI
ROYAL
Major Points:
- variable – For each iteration, the value of the next property is assigned to the variable.
- Variable can be declared with const, let, or var.
- iterable – An object that has iterable properties.
Conclusion
Now we completely discuss about How JavaScript For Loop Works, with the help of example and complete explanations of example and also we check the different types of loop which is used for loop through array, loop through array of objects and JavaScript iterate array.
Also Check:
- 2 Ways to Open URL in New Tab Using JavaScript
- Check If String Contains Exact Match in JavaScript
- Check If Folder Or File Exists Using JavaScript
- Auto Add Country Code in Input using JavaScript
Check for more https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration
Happy Coding..!
[…] How JavaScript For Loop Works (Complete Guide With Example) […]
[…] How JavaScript For Loop Works (Complete Guide With Example) […]
[…] How JavaScript For Loop Works (Complete Guide With Example) […]
[…] How JavaScript For Loop Works (Complete Guide With Example) […]
[…] How JavaScript For Loop Works (Complete Guide With Example) […]
[…] How JavaScript For Loop Works (Complete Guide With Example) […]
[…] How JavaScript For Loop Works (Complete Guide With Example) […]
[…] How JavaScript For Loop Works (Complete Guide With Example) […]
[…] How JavaScript For Loop Works (Complete Guide With Example) […]