for loop vs forEach in javascript

Farhan Taher
May 5, 2021

for loop at a glance:
For loop variable only use int only.
Repeat until the expression evaluates false.
For loop is faster than forEach
for(int i = 1; i <= 5; i++){

console.log(i);//print 1 to 5
}
forEach at a glance
forEach variable same as the type of values under an array
It is used as a collection of an array or collection of objects.
It is slower than for loop.
let nums = [1, 2, 3];

nums.forEach(num => console.log(num * 5))//print 5, 10, 15

--

--