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. T…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Mitchell Mutandah

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

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

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

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

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

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

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

cheers


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Mitchell Mutandah


Print Share Comment Cite Upload Translate Updates
APA

Mitchell Mutandah | Sciencx (2023-01-08T13:55:52+00:00) 6 Ways to loop over arrays in Javascript. Retrieved from https://www.scien.cx/2023/01/08/6-ways-to-loop-over-arrays-in-javascript/

MLA
" » 6 Ways to loop over arrays in Javascript." Mitchell Mutandah | Sciencx - Sunday January 8, 2023, https://www.scien.cx/2023/01/08/6-ways-to-loop-over-arrays-in-javascript/
HARVARD
Mitchell Mutandah | Sciencx Sunday January 8, 2023 » 6 Ways to loop over arrays in Javascript., viewed ,<https://www.scien.cx/2023/01/08/6-ways-to-loop-over-arrays-in-javascript/>
VANCOUVER
Mitchell Mutandah | Sciencx - » 6 Ways to loop over arrays in Javascript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/01/08/6-ways-to-loop-over-arrays-in-javascript/
CHICAGO
" » 6 Ways to loop over arrays in Javascript." Mitchell Mutandah | Sciencx - Accessed . https://www.scien.cx/2023/01/08/6-ways-to-loop-over-arrays-in-javascript/
IEEE
" » 6 Ways to loop over arrays in Javascript." Mitchell Mutandah | Sciencx [Online]. Available: https://www.scien.cx/2023/01/08/6-ways-to-loop-over-arrays-in-javascript/. [Accessed: ]
rf:citation
» 6 Ways to loop over arrays in Javascript | Mitchell Mutandah | Sciencx | https://www.scien.cx/2023/01/08/6-ways-to-loop-over-arrays-in-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.