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

 nodejs loop

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 :

 nodejs loop code

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 :

 nodejs loop 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

Related Searches to node.js - Loops

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add wikitechy.com to your ad blocking whitelist or disable your adblocking software.

×