will javascript foreach work when the for loop doesn’t?

There are several differences between forEach and using a for loop

The most obvious is how they deal with sparse arrays. forEach wont try to run the callback if it hits an empty array element but for will and the array item will give undefined.

On the other side, you can’t just break out of a forEach as you can a for loop.

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach for further details.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top