javascript tutorial - [Solved-5 Solutions] For-each over an array in javascript - javascript - java script - javascript array
Problem
How can I loop through all the entries in an array using JavaScript?
Solution 1:
forEach(instance in theArray)
Solution 2:
TL;DR
Don't use for-in
unless you use it with safeguards or are at least aware of why it might bite you.
Your best bets are usually
- Don't use
for-in
unless you use it with safeguards or are at least aware of why it might bite you. - Your best bets are usually a
for-of
loop (ES2015+ only),Array#forEach
(spec | MDN) (or its relativessome
and such) (ES5+ only),a simple old-fashionedfor
loop,orfor-in
with safeguards.
Solution 3:
The standard way to iterate an array in JavaScript is a vanilla for
-loop: