Ever since its introduction in ECMA-262, 3rd Edition, the Array.prototype.push
method's return value is a Number
:
15.4.4.7 Array.prototype.push ( [ item1 [ , item2 [ , … ] ] ] )
The arguments are appended to the end of the array, in the order in which they appear. The new length of the array is returned as the result of the call.
What were the design decisions behind returning the array's new length, as opposed to returning something potentially more useful, like:
Why was it done like this, and is there a historical record of how these decisions came to be made?
I posted this in TC39's communication hub, and was able to learn a bit more about the history behind this:
push
,pop
,shift
,unshift
were originally added to JS1.2 (Netscape 4) in 1997.There were modeled after the similarly named functions in Perl.
JS1.2 push followed the Perl 4 convention of returning the last item pushed. In JS1.3 (Netscape 4.06 summer 1998) changed push to follow the Perl 5 conventions of returning the new length of the array.
/* * If JS1.2, follow Perl4 by returning the last thing pushed. Otherwise, * return the new array length. */