In this tutorial, you will learn about for
loop in javascript and how you can use it. You will learn javascript for loop with the help of examples.
Loops, we used to execute a block code a number of times. This means you can set the number for how many times you want to repeat the code.
Let’s say you want to print the same value multiple times for a specific condition. Then you can use the for
loop and make a condition and print the value until matches the condition for the specific number of times given.
There are many other types of loops in javascript like for loop, for in loop, for of loop, while loop, and do-while loop. But today we will focus on the for
loop, the rest we will cover in other tutorials.
See Also: How to Convert Html to PDF using JavaScript?
JavaScript For Loop
As I said, for loop is used to repeat the code multiple times for a specific number. So let’s understand the syntax of the for
loop and how you can use it.
Syntax
initialStatement: used to declare the variable and it will only execute once.
conditionStatement: used to define the condition and execute the code. if the condition is true
code will be executed, if the condition is false
the code will be stopped.
updateStatement: it will execute every time when the condition is true
and update the value of initialStatement (initial value).
Let’s take a simple example of for loop to print the 1 to 10 numbers.
Output
1 2 3 4 5 6 7 8 9 10
The above code will print the number from 1 to 10. You can print numbers, strings, or array values in the loop.
Array For Loop in JavaScript
Let’s take an example of array values to print them with the help of for loop. Yes, you can print the value of the array using for loop, you have to get the length of array elements using .length
and make a condition with that.
Output
Mandeep Prabh Harleen Ravinder Ramesh
In the above code, we declare the students
array and then use for loop to print the value of it. We take the value of i is 0 because an array always starts from a 0 index then we made the condition that to print all values of an array.
Break For Loop in JavaScript
You can also use the break
statement to terminate the execution. You just have to make the condition to stop the execution of code and then pass the break
statement in that condition.
Let’s see the example of a break statement in for loop.
Output
Mandeep Prabh Harleen
As you can see from the above code, it will only print the first three values of the array. Because we made a condition with a break statement to stop the execution of code.
Infinite For Loop in JavaScript
To print the value infinite times, you have to make the infinite for loop condition. Whenever the condition is true the loop will continue until the browser memory is full.
Output
1 2 3 4 5 ... ... ...
You can see in the above code, we made a condition that will always be true, so it will print the infinite numbers until browser memory is full.
Nested For Loop in JavaScript
You can also print the value of a nested array, which means if you have a multi-dimensional array then you can use the for loop to print the value of it.
See the following example of a nested array with for loop.
Output
HTML CSS Bootstrap ---------- PHP JavaScript Python ---------- Laravel Django WordPress ----------
Same you can make conditions as you want to print the value of the array and can use more array groups inside the main array, how you want to print is up to you how you can handle it.
Reverse For Loop in JavaScript
To print the value in reverse order, you just have to make a condition accordingly, so the loop will start from end to start. See the following example of reverse for loop.
Output
5 4 3 2 1
As you can see it will print the backward array value. We take the value of i
as the length of the array and minus 1, so it will be 4 and start from the 4th index, continue.
That’s it!!!
Hope you like it. Please share with your group and if you have any queries please ask me in the comment section, I will respond to you as soon as possible.