Home > Web Front-end > JS Tutorial > Why Does Array.prototype.push Return the New Array Length Instead of Useful Data?

Why Does Array.prototype.push Return the New Array Length Instead of Useful Data?

Patricia Arquette
Release: 2024-10-22 09:43:03
Original
973 people have browsed it

Why Does Array.prototype.push Return the New Array Length Instead of Useful Data?

Why Array.prototype.push Returns the New Length Instead of Useful Data

Array.prototype.push, introduced in ECMA-262, 3rd Edition, returns a Number representing the new length of the array after elements are appended. This design choice has perplexed developers, as it's not directly useful information.

Historical evidence from TC39's communication channels sheds light on this decision. In JS1.2 (Netscape 4), push was modeled after Perl's function and behaved similar to Perl 4 by returning the last item pushed. However, with JS1.3 (Netscape 4.06), push underwent a change to align with Perl 5 conventions and now returns the new array length.

According to the original jsarray.c source code, this modification was intentional:

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

This design choice may stem from a desire to provide consistency across versions of JavaScript and align with the established Perl 5 behavior. While returning the new length may not be immediately valuable, it does ensure clarity on the size of the array after the push operation.

The above is the detailed content of Why Does Array.prototype.push Return the New Array Length Instead of Useful Data?. For more information, please follow other related articles on the PHP Chinese website!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template