For Loop | JavaScript

For Loop | JavaScript

For loop in JavaScript

The ‘for’ loop is the most concise form of all loops. It consists of three main parts:

  • loop initialization: create an index variable which serves as a counter for the number of times it has been executed. The initialization statement is executed before the loop begins.
  • loop condition: is the condition for continuing the loop and it stops the loop as soon as the condition is false. - increment/decrements condition: This is what allows you to keep track of the number of loop executions.

Image for post

Using for loop in arrays — “for… in” and “for … of”

There are many situations in which you will have an array and will need to work your way through it, doing something with each element. There are two easier ways of looping through (or iterating over) arrays.

The **for****in** loop works in a similar way to the above normal **for** loop example, but is easier to read, and does all the iteration work for you. It is used to loop through an object’s properties in an arbitrary(random) way.

For cases where the specific index of an element is not needed during iteration, you can use a **for** **of** loop. It loops over data structures that are iterable such as Arrays, strings, Maps, Sets, and more.

Image for post

Understanding the usage of for ,For...of and For...in loop can save you a lot of time during development. I believe this article helped you understand and write better loop constructs in your JavaScript development.

Happy Coding…