Loops in JavaScript [for]

Loops in JavaScript [for]

for loop in JavaScript

  • There are four types of [for] loops in JavaScript and they are as follows
  1. for loop

  2. for in loop

  3. for of loop

For loop -

  • The for statement creates a loop with 3 optional expressions.

SYNTAX -

for (expression 1; expression 2; expression 3;) {
// code block to be executed;
}

For in loop -

  • The JavaScript for in statement loops through the properties of an Object.

SYNTAX -

for (key in object) {
// code block to be executed;
}

For of loop -

  • The JavaScript for of statement loops through the values of an iterable object.

It lets you loop over iterable data structures such as Arrays, Strings, Maps, NodeLists, and more.

SYNTAX -

for (variable of iterable) {
// code block to be executed;
}

Conclusion -

Understanding for loop and its variations.