aweber-banner
for and forEach loops in javascript

for and forEach loops in javascript

In this tutorial, we will learn about some basic about for and forEach loops of javascript.

For Loop:

Loops offer a quick and easy way to do something repeatedly.

for(let count=0; count < 5;count++){
   console.log('Number'+count);
}

This standard for loop will print out the string 'Number' five times. The value of count increments each time the loop executes if the count is not larger than four (4).

PROS and CONS:

Pros:

  • work with all browsers.
  • we can use break and continue flow control statements

Cons:

  • Too verbose.
  •  Imperative

forEach loop:

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

const sampleArr = ['a','b','c','d','e'];

sampleArr.forEach(element => console.log(element)); 

PROS and CONS:

Pros:

  • Working all browsers
  • Declarative

Cons:

  • we cannot use break and continue flow control statements

Subscribe to our Newsletter

Stay up to date! Get all the latest posts delivered straight to your inbox.

If You Appreciate What We Do Here On TutsCoder, You Should Consider:

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

Leave a Comment