Loop and Iteration in JavaScript

/>

What is loop and iteration?

Loops offer a quick and easy way to do something repeatedly. In this article we introduces the different iteration statements available to JavaScript.

There are many different kinds of loops, but they all essentially do the same thing: they repeat an action some number of times. (Note that it's possible that number could be zero!)

 javascript loop and itaration image

The various loop mechanisms offer different ways to determine the start and end points of the loop. There are various situations that are more easily served by one type of loop over the others.

JavaScript are provide some statements for loops - see in below

for statement

A for loop repeats until a specified condition evaluates to false. The JavaScript for loop is similar to the Java and C for loop.

A for statement syntex looks

    for (initialization; condition; afterthought)
   statement

When a for loop executes, the following occurs:

  • The initializing expression initialization, if any, is executed. This expression usually initializes one or more loop counters, but the syntax allows an expression of any degree of complexity. This expression can also declare variables.
  • The condition expression is evaluated. If the value of condition is true, the loop statements execute. Otherwise, the for loop terminates. (If the condition expression is omitted entirely, the condition is assumed to be true.)
  • The statement executes. To execute multiple statements, use a block statement ({ }) to group those statements.
  • If present, the update expression afterthought is executed.
  • Control returns to Step 2.

Example

Loops are handy, if you want to run the same code over and over again, each time with a different value.
Often this is the case when working with arrays:

    var Array=[1,2,3,4,5,6,7,8];
for (let i = 0; i < Array.length; i++) {
     console.log(Array[i]);
}
In this example for loops print All the value in the Array
Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.