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

Topics covered:

Found this article helpful?

TutsCoder tutorials are free and ad-light — supported by readers like you. Buy me a coffee (or two ☕☕) as a token of appreciation and help keep Angular & Node.js content coming!

One-time. No subscription. 100% optional. 🙏 Every coffee counts!

Leave a Comment

Your email will not be published. Spam-free zone. ✌️

Available for Projects

Need Help With Your
Angular or Node.js Project?

7+ years of MEAN Stack experience. I build scalable Angular 21 apps, Node.js APIs, and SaaS products — delivered on time, every time.

7+ Years MEAN Stack Angular 21 + Nx Expert 20+ Projects Delivered Remote / Freelance