6 Ways to loop over arrays in Javascript
Tags: javascript, beginners, programming, tutorial
Published: 2023-01-08 13:55:52


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.

```js
//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

```

## 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.

```js
//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

```

## 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.

```js
//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

```

## 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.

```js
//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

```


## 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.

```js
//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

```


## 6. The For Each Loop
The **forEach()** method executes a provided function once for each array element.

```js
//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

```


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

![cheers](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h9f3y1kohj0sj6teaeat.jpeg)