Node js - Loops - nodejs - node js tutorial - node
What is nodejs loops?
- nodejs loop Provides a controlled looping of data.
- Is one of the familiar programming language control statements.
- Specify iteration of code.
- Allows the code to be executed repeatedly.
- Is often associated with loop variable to allow the body of loop to know about the sequencing.
- Is used in where the number of iterations is predefined without any condition.
The traditional for-loop got 3 parts :
- 1.the initialization,
- 2.the condition, and finally
- 3.the increment part.
Syntax :
for (INITIALIZATION; CONDITION; INCREMENT)
{
// Code for the for-loop's body goes here.
}
Explanation :
1.The initialization :
- a.declares the variables with an initial value.
- b.type of a variable should be same for all variables initialized here.
2.The condition :
- a.checks out the condition and proceeds on success.
- b.quits the loop if the condition fails.
3.The increment :
- a.increase or decrease so the initialized variable.
- b.depends on the condition part to proceed or stop its duty.
The above scenarios are explained in fig

Learn Nodejs - Nodejs tutorial - nodejs loop - Nodejs examples - Nodejs programs
For loop sample code :
var x=0;
for (i=0;i<=5;i++)
{
x++;
console.log("The value of x is : "+x);
}
Code Explanation :

Learn Nodejs - Nodejs tutorial - nodejs loop code - Nodejs examples - Nodejs programs
- x is declared with a value 0.
- A for loop is stated with i’s initial value that is 0 and with a condition to check whether i<=5 and with an increment statement to increase the value of i.
- The value of x is incremented and the same is sent for output to console with the line of code “console.log("The value of x is : "+x);
Node JS Output :

Learn Nodejs - Nodejs tutorial - nodejs loop output - Nodejs examples - Nodejs programs
- Executing the for loop in the nodejs framework produces the output of the variable x as shown here as:
The value of x is : 1
The value of x is : 2
The value of x is : 3
The value of x is : 4
The value of x is : 5
The value of x is : 6