Because push
returns the size of the array:
The push() method adds new items to the end of an array, and returns
the new length.
It does execute the push function, it’s just not returning what you were expecting.
JavaScript was written in only 10 days so we are stuck with some design decisions. It’s amazing that we even get as much functional style as we do (since marketing people wanted a “Java-like” language).
JavaScript enables one to write in a functional way, but there may be some roadblocks.
This quote is interesting:
Luckily Eich managed to sneak some great ideas into the language
despite what he was asked to do.
It is also not totally clear-cut whether it would be better for push
to return the array length
or what you had just pushed, or the whole array reference…etc.
solved why cannot javascript execute methods inside methods?