DEV Community

Cover image for Arrays iteration in JavaScript
Ukobaandrew
Ukobaandrew

Posted on

Arrays iteration in JavaScript

Introduction

Array iteration in JavaScript also known as Loops can execute a block of code a number of times.
Loops are useful if you wish to repeat the same code with a different value each time.

You can now write in simple lines and let loops do the job for you.

Example: This is frequently the case while writing an array, if we are to code different array iteration values;

const foods= array[fish, pizza, meat, rice, beans];

text += foods[0] + "<br>"; 

text += foods[1] + "<br>"; 
text += foods[2] + "<br>"; 
text += foods[3] + "<br>"; 
text += foods[4] + "<br>"; 
Enter fullscreen mode Exit fullscreen mode

looping has made it so easy and fluent also time efficient instead of typing same code a number of time,
You can simply type in few lines of coding using loops
:

const foods= array[fish, pizza, meat, rice, beans];
for(i=0; i<foods.length; i++){
console.log(i)

}
Enter fullscreen mode Exit fullscreen mode

Different Kinds of Loops
JavaScript supports different kinds of loops:

  1. for - loops through a block of code a number of times
  2. for/in - loops through the properties of an object
  3. for/of - loops through the values of an iterable object
  4. while - loops through a block of code while a specified condition is true
  5. do/while - also loops through a block of code while a specified condition is true.

USES AND BENEFITS
-Loops can execute a block of code a number of times
-Looping saves time and stress
-looping can be achieved in just few lines of coding.


The For Loop


The for loop is often the tool you will use when you want to create a loop.

The for loop has the following syntax:


for (statement 1; statement 2; statement 3) {
    code block to be executed
}
Enter fullscreen mode Exit fullscreen mode

Statement 1 is executed before the loop (the code block) starts.

Statement 2 defines the condition for running the loop (the code block).

Statement 3 is executed each time after the loop (the code block) has been executed.

Example


for (i = 0; i < 6; i++) {
    text += "The number is " + i + "<br>";
}
Enter fullscreen mode Exit fullscreen mode

From the example above, you can read:

Statement 1: sets a variable before the loop starts (var i = 0).

Statement 2
: defines the condition for the loop to run (i must be less than 6).

Statement 3: increases a value (i++) each time the code block in the loop has been executed.

Statement 1
Normally you will use statement 1 to initiate the variable used in the loop (i = 0).

This is not always the case, JavaScript doesn't care. Statement 1 is optional.

You can initiate many values in statement 1 (separated by comma):

Example


for (i = 0, len = foods.length, text = ""; i < len; i++) {
    text += foods[i] + "<br>";
}
Enter fullscreen mode Exit fullscreen mode

And you can omit statement 1 (like when your values are set before the loop starts):


Example

var i = 2;
var len =foods.length;
var text = "";
for (; i < len; i++) {
    text += cars[i] + "<br>";
}
Enter fullscreen mode Exit fullscreen mode

Statement 2
Often statement 2 is used to evaluate the condition of the initial variable.

This is not always the case, JavaScript doesn't care. Statement 2 is also optional.

If statement 2 returns true, the loop will start over again, if it returns false, the loop will end.

Note If you omit statement 2, you must provide a break inside the loop. Otherwise the loop will never end. This will crash your browser. Read about breaks in a later chapter of this tutorial.
Statement 3
Often statement 3 increases the initial variable.

This is not always the case, JavaScript doesn't care, and statement 3 is optional.

Statement 3 can do anything like negative increment (i--), positive increment (i = i + 2), or anything else.

Statement 3 can also be omitted (like when you increment your values inside the loop):

Example

var i = 0;
var len = foods.length;
for (; i < len; ) {
    text += foods[i] + "<br>";
    i++;
}
Enter fullscreen mode Exit fullscreen mode

The For/In Loop


The JavaScript for/in statement loops through the properties of an object:

Example

var person = {fname:"Andrew", lname:"Elijah", age:30};
var text = "";
var x;
for (x in person) {
    text += person[x];
}
Enter fullscreen mode Exit fullscreen mode

JavaScript While Loop

Loops can execute a block of code as long as a specified condition is true.

The While Loop

The while loop loops through a block of code as long as a specified condition is true.

Syntax

while (condition) {
    code action to be executed
}
Enter fullscreen mode Exit fullscreen mode

Example
In the following example, the code in the loop will run, over and over again, as long as a variable (i) is less than 100:

Example

while (i < 100) {
    text += "The number is " + i;
    i++;
}
Enter fullscreen mode Exit fullscreen mode

Note If you forget to increase the variable used in the condition, the loop will never end. This will crash your browser.


The Do/While Loop


The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.

Syntax

do {
    code block to be executed
}
while (condition);
Enter fullscreen mode Exit fullscreen mode

Example
The example below uses a do/while loop. The loop will always be executed at least once, even if the condition is false, because the code block is executed before the condition is tested:

Example

do {
    text += "The number is " + i;
    i++;
}
while (i < 10);
Enter fullscreen mode Exit fullscreen mode

Do not forget to increase the variable used in the condition, otherwise the loop will never end!

Summary

The while loop is much the same as a for loop, with statement 1 and statement 3 omitted.

The loop in this example uses a for loop to collect the car names from the cars array:

Example

var days = ["Mondays", "Tuesdays", "Fridays", "Sundays"];
var i = 0;
var text = "";

for (;days[i];) {
    text += days[i] + "<br>";
    i++;
}
Enter fullscreen mode Exit fullscreen mode

The loop in this example uses a while loop to collect the car names from the cars array:

Example

var days = ["Mondays", "Tuesdays", "Fridays", "Sundays"];
var i = 0;
var text = "";

while (days[i]) {
    text += days[i] + "<br>";
    i++;
}
Enter fullscreen mode Exit fullscreen mode

so the loops are similar and can perform the same action if coded properly!!!!!

Oldest comments (0)