for loop :
The most popular loop in javascript is for loop.
It allows us to run a piece of code repeatedly for specific number of iterations.
Three main parts of this loop are: Initialization, condition, and iteration.
Let's examine each part of the for loop individually:
Initialization :
This is the first action that is taken before the loop begins and is only performed once.
The variable or variables in the loop are initialized using it. Declaring variables and
assigning them can be included. For instance, the statement let i = 0 gives the loop variable
i a value of 0.
Condition :
Before beginning each iteration of the loop, this condition is checked. The body of the loop is
run if the condition evaluates to true. The loop is ended if the condition evaluates to false.
Any expression that yields the boolean value true or false is acceptable but runs the block
only for true cases.
i < 5, for instance,
determine whether the loop variable i is smaller than 5.
Iteration: This is the step that is executed at the end of each iteration of the loop. It is
used to update the loop variable or variables. It can include incrementing or decrementing the
loop variable, or modifying any other relevant values. For example, i++ increments the loop
variable i by 1 after each iteration
The curly braces make up the body of the loop.
As many times conditions will be evaluated as true, that many times the code block inside this
body will be executed repeatedly.
Here is an illustration showing how to use a for loop:
for (let i = 0; i < 5; i++) { console.log(i); }
In this example, the for loop initializes the loop variable i with a value of 0. The condition
checks if i is less than 5. If the condition is true, the loop body is executed. In each
iteration, the value of i is printed to the console using console.log(). After each iteration,
the i variable is incremented by 1 (i++). This process continues until i reaches 5, at which
point the condition evaluates to false and the loop terminates.
When you run this code, the following output will be displayed in the console:
0 1 2 3 4
This is because the loop iterates five times, starting from 0 and ending at 4, with i
incrementing by 1 in each iteration.
You can use the for loop to perform a wide range of tasks, such as iterating over arrays,
executing a block of code a specific number of times, and more. The flexibility of the for
loop makes it a powerful tool for repetitive operations in JavaScript.
Lets see some real programming in action:
Write a javascript program to calculate sum of numbers from 1 to n, where value of n will be
provided.
function calculateSum(number) { let sum = 0; for (let i = 1; i <= number; i++) { sum += i; } return sum; } // Testing the function const inputNumber = 5; const result = calculateSum(inputNumber); console.log("The total sum of all the numbers from 1 to", inputNumber, "is:", result);
In this example, the calculateSum function takes a number as input and uses a for loop to
iterate from 1 up to the given number. The variable sum is initialized to 0. In each iteration,
the loop adds the current value of i to the sum using the += operator. After the loop
finishes, the function returns the final sum.
To test the function, we set the inputNumber variable to 5 and call the calculateSum function
with this value. The result is stored in the result variable. Finally, we display the
result using console.log().
When you run this code, the console will show the following output:
The total sum of all the numbers from 1 to 5 is: 15
This is because the function calculates the sum of numbers from 1 to 5, which
is 1 + 2 + 3 + 4 + 5 = 15.
Feel free to modify the inputNumber variable to test the function with different input values
and observe the corresponding sums calculated using the for loop.