JavaScript array loop | Ways of iterating over an array

JavaScript array supports almost all loop statements. Using Loops you can iterate over an array. Means do something repeatedly or execute a block of code a number of times.

Loops are very useful you can run the same code over and over again, each time with a different value.

Supporting Loops statements in Javascript

Here are some statements for loops provided in JavaScript are:

  • for statement
  • do…while statement
  • while statement
  • forEach method

Examples of JavaScript array loops

Let’s see the examples of Array work with for, do-while, while, etc.

for loop statement

A for loop repeats until a specified condition evaluates to false.

Output:

Examples of JavaScript array loops

Using a while loop

A while statement executes its statements as long as a specified condition evaluates to true.

forEach method

The forEach method calls the provided function once for every array of items in the order.

do…while statement

The do…while statement repeats until a specified condition evaluates to false.

Output: 12345

Do comment if you have any doubts, questions, and suggestions on this tutorial. All loops are almost similar to other programming languages.

Note: The All JS Examples codes are tested on the Safari browser (Version 12.0.2) and Chrome.
OS: macOS 10.14 Mojave
Code: HTML 5 Version


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.