History of Array.prototype.push returning array length

Per the Standard ECMA-262 3rd edition (published in 1999), the first definition for Array.prototype.push is:

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.

That is, the original definition of this method returns the length of the new array after its call, as opposed to a reference to the newly appended item(s), or the mutated array itself.

I came across a StackOverflow post asking about the history of the decision to return the array length, but didn't find a compelling answer.

I'm not sure if this is the correct place to ask about this but figured it was worth a shot.

Can anyone provide any insight into the history of this decision, and/or why that choice was made over some of the alternatives?

This is the perfect place to ask such a thing. I went digging through Ecma's archives and didn't find much discussion of .push() beyond the spec itself, but I wonder if someone with a lot of historical knowledge might know. I've stated asking around!

1 Like

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.

see https://dxr.mozilla.org/classic/source/js/src/jsarray.c#804

     * If JS1.2, follow Perl4 by returning the last thing pushed.  Otherwise,
     * return the new array length.

Thanks for the reply! Of course, this partly begs the question "Why did Perl change that between 4 and 5?" but I figured the answer was mostly related to being influenced by another language.