(JavaScript Tutorials for Beginners)
In this end-to-end example, you will learn – JavaScript tutorials for Beginners – JavaScript for loop.
JavaScript for loop
In this tutorial, you will learn about the loops and about for loops in JavaScript with the help of examples.
In programming, loops are used to repeat a block of code.
For example, if you want to show a message 100 times, then you can use a loop. It’s just a simple example; you can achieve much more with loops.
This tutorial focuses on JavaScript for
loop. You will learn about the other type of loops in the upcoming tutorials.
JavaScript for loop
The syntax of the for loop is:
for(initialExpression; condition; updateExpression) {
// for loop body
}
Here,
- The initialExpression initializes and/or declares variables and executes only once.
- The condition is evaluated.
- If the condition is
false
, thefor
loop is terminated. - if the condition is
true
, the block of code inside of thefor
loop is executed.
- If the condition is
- The updateExpression updates the value of initialExpression when the condition is
true
. - The condition is evaluated again.This process continues until the condition is
false
.
To learn more about the conditions, visit JavaScript Comparison and Logical Operators.
Example 1: Display a Text Five Times
// program to display text 10 times
let n = 5;
// looping from i = 1 to 5
for (let i = 1; i <= n; i++) {
console.log(`I love JavaScript.`);
}
Output
I love JavaScript. I love JavaScript. I love JavaScript. I love JavaScript. I love JavaScript.
Here is how this program works.
Iteration | Variable | Condition: i <= n | Action |
---|---|---|---|
1st | i = 1 n = 5 |
true |
I love JavaScript. is printed. i is increased to 2. |
2nd | i = 2 n = 5 |
true |
I love JavaScript. is printed. i is increased to 3. |
3rd | i = 3 n = 5 |
true |
I love JavaScript. is printed. i is increased to 4. |
4th | i = 4 n = 5 |
true |
I love JavaScript. is printed. i is increased to 5. |
5th | i = 5 n = 5 |
true |
I love JavaScript. is printed. i is increased to 6. |
6th | i = 6 n = 5 |
false |
The loop is terminated. |
Example 2: Display numbers from 1 to 5
// program to display numbers from 1 to 5
let n = 5;
// looping from i = 1 to 5
// in each iteration, i is increased by 1
for (let i = 1; i <= n; i++) {
console.log(i); // printing the value of i
}
Output
1 2 3 4 5
Here is how this program works.
Iteration | Variable | Condition: i <= n | Action |
---|---|---|---|
1st | i = 1 n = 5 |
true |
1 is printed. i is increased to 2. |
2nd | i = 2 n = 5 |
true |
2 is printed. i is increased to 3. |
3rd | i = 3 n = 5 |
true |
3 is printed. i is increased to 4. |
4th | i = 4 n = 5 |
true |
4 is printed. i is increased to 5. |
5th | i = 5 n = 5 |
true |
5 is printed. i is increased to 6. |
6th | i = 6 n = 5 |
false |
The loop is terminated. |
Example 3: Display Sum of n Natural Numbers
// program to display the sum of natural numbers
let sum = 0;
let n = 100
// looping from i = 1 to n
// in each iteration, i is increased by 1
for (let i = 1; i <= n; i++) {
sum += i; // sum = sum + i
}
console.log('sum:', sum);
Output
sum: 5050
Here, the value of sum is 0 initially. Then, a for
loop is iterated from i = 1 to 100
. In each iteration, i is added to sum and its value is increased by 1.
When i becomes 101, the test condition is false
and sum will be equal to 0 + 1 + 2 + … + 100.
The above program to add sum of natural numbers can also be written as
// program to display the sum of n natural numbers
let sum = 0;
let n = 100;
// looping from i = n to 1
// in each iteration, i is decreased by 1
for(let i = n; i >= 1; i-- ) {
// adding i to sum in each iteration
sum += i; // sum = sum + i
}
console.log('sum:',sum);
This program also gives the same output as the Example 3. You can accomplish the same task in many different ways in programming; programming is all about logic.
Although both ways are correct, you should try to make your code more readable.
JavaScript Infinite for loop
If the test condition in a for
loop is always true
, it runs forever (until memory is full). For example,
// infinite for loop
for(let i = 1; i > 0; i++) {
// block of code
}
In the above programs, the condition is always true
which will then run the code for infinite times.
In the next tutorial, you will learn about while
and do...while
loop.
Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.
Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners
Latest end-to-end Learn by Coding Projects (Jupyter Notebooks) in Python and R:
All Notebooks in One Bundle: Data Science Recipes and Examples in Python & R.
End-to-End Python Machine Learning Recipes & Examples.
End-to-End R Machine Learning Recipes & Examples.
Applied Statistics with R for Beginners and Business Professionals
Data Science and Machine Learning Projects in Python: Tabular Data Analytics
Data Science and Machine Learning Projects in R: Tabular Data Analytics
Python Machine Learning & Data Science Recipes: Learn by Coding
R Machine Learning & Data Science Recipes: Learn by Coding
Comparing Different Machine Learning Algorithms in Python for Classification (FREE)
There are 2000+ End-to-End Python & R Notebooks are available to build Professional Portfolio as a Data Scientist and/or Machine Learning Specialist. All Notebooks are only $29.95. We would like to request you to have a look at the website for FREE the end-to-end notebooks, and then decide whether you would like to purchase or not.
JavaScript tutorials for Beginners – JavaScript while and do…while Loop
Pandas Example – Write a Pandas program to remove infinite values from a given DataFrame
Learn Java by Example: Java Program to Calculate the Sum of Natural Numbers