Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why was Array.prototype.forEach designed to skip undefined values?

The doc of Array.prototype.forEach says:

forEach executes the provided callback once for each element of the array with an assigned value.
It is not invoked for indexes which have been deleted or which have been initialized to undefined.

Why?

like image 411
flowerszhong Avatar asked Oct 19 '25 04:10

flowerszhong


1 Answers

Look at this console "experiment" I ran:

Firebug console

Image link

You can clearly see that Array.forEach does not skip undefined values (that the key here). Only undefined as in deleted will be skipped.

like image 86
moritzpflaum Avatar answered Oct 21 '25 16:10

moritzpflaum