I like Array.forEach()
and use it whenever I need to iterate through elements. However one needs to be aware of its quirk.
|
|
A typical for
outputs all elements including those not initialized yet (hold your breath for a moment on the initialized thing).
|
|
Now, repeat the same for forEach
.
|
|
Hmm.. that is strange.
Let’s use a fill
with array but not assign any value.
|
|
What is this black magic?
I am flabbergasted. Is forEach
right for me?
Yes.
forEach
is pretty, smart and committed (to the future of Javascript). You can both live a happy life.
A better question to think about is why we initialize empty arrays like crazy.