DEV Community

Cover image for 6 Ways to loop over arrays in Javascript
Mitchell Mutandah
Mitchell Mutandah

Posted on

6 Ways to loop over arrays in Javascript

Hello friends. In this script, I'm going to briefly talk about 6 loops in JS. So let's get started!

Loops offer a quick and easy way to do something repeatedly. They can execute a block of code as long as a specified condition is true.

1. The While Loop

The while statement creates a loop that executes a specified statement as long as the test condition evaluates to true. The condition is evaluated before executing the statement.

//Example

const menu = ['Rice', 'Chicken', 'Burger', 'Pizza', 'Cheese cake'];

let i = 0;
while(i < menu.length) {
   console.log('I will eat' + menu[i])
   i++;
}

//Results
I will eat Rice
I will eat Chicken
I will eat Burger
I will eat Pizza
I will eat Cheese cake

Enter fullscreen mode Exit fullscreen mode

2. The Do While Loop

The do...while statement creates a loop that executes a specified statement until the test condition evaluates to false. The condition is evaluated after executing the statement, resulting in the specified statement executing at least once.

//Example

const menu = ['Rice', 'Chicken', 'Burger', 'Pizza', 'Cheese cake'];

let i = 0;
do {
   console.log('I will eat' + menu[i]);
   i++;
}
while(i < menu.length);

//Results
I will eat Rice
I will eat Chicken
I will eat Burger
I will eat Pizza
I will eat Cheese cake

Enter fullscreen mode Exit fullscreen mode

3. The For Loop

The for statement creates a loop with 3 optional expressions:
for (expression 1; expression 2; expression 3) {
// code block to be executed
}

Expression 1 is executed (one time) before the execution of the code block.
Expression 2 defines the condition for executing the code block.
Expression 3 is executed (every time) after the code block has been executed.

//Example

const menu = ['Rice', 'Chicken', 'Burger', 'Pizza', 'Cheese cake'];

for(let i = 0; i < menu.length; i++;) {
   console.log('I will eat' + menu[i]);

}


//Results
I will eat Rice
I will eat Chicken
I will eat Burger
I will eat Pizza
I will eat Cheese cake

Enter fullscreen mode Exit fullscreen mode

4. The For In Loop

The for...in statement iterates over all enumerable string properties of an object (ignoring properties keyed by symbols), including inherited enumerable properties.

//Example

const menu = ['Rice', 'Chicken', 'Burger', 'Pizza', 'Cheese cake'];

for(const index in menu) {
   console.log('I will eat' + menu[index]);  
}


//Results
I will eat Rice
I will eat Chicken
I will eat Burger
I will eat Pizza
I will eat Cheese cake

Enter fullscreen mode Exit fullscreen mode

5. The For of Loop

The for...of statement executes a loop that operates on a sequence of values sourced from an iterable object. It lets you loop over iterable data structures such as Arrays, Strings, Maps, NodeLists, and more.

//Example

const menu = ['Rice', 'Chicken', 'Burger', 'Pizza', 'Cheese cake'];

for(const item of menu) {
   console.log('I will eat' + item);  
}


//Results
I will eat Rice
I will eat Chicken
I will eat Burger
I will eat Pizza
I will eat Cheese cake

Enter fullscreen mode Exit fullscreen mode

6. The For Each Loop

The forEach() method executes a provided function once for each array element.

//Example

const menu = ['Rice', 'Chicken', 'Burger', 'Pizza', 'Cheese cake'];


menu.forEach(item => {
   console.log('I will eat' + item); 
})

//Results
I will eat Rice
I will eat Chicken
I will eat Burger
I will eat Pizza
I will eat Cheese cake

Enter fullscreen mode Exit fullscreen mode

Let me know in the comments section what you think about JS loops.
That's it!! #HappyCoding

cheers

Top comments (0)