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