Getting last element of Array ?
- Stage: Unstaged
- Status: Withdrawn
- ECMAScript edition: —
- Synchronized: Aug 28, 2026
- 中文译文 · Source repository
This proposal addresses the common but error-prone pattern of accessing the last element of an array via array\[array.length - 1\]. It proposes two new properties on Array.prototype: lastItem as a getter/setter and lastIndex as a getter, with generic algorithms that also work on array-like objects.
The README below comes from the upstream repository and may contain outdated stage or status metadata. Use the proposal details above as the current source of truth.
Getting last item from Array
This proposal's champion (@keithamus) does not plan to advance to Stage 2 for now. Other proposals (Array.prototype.item and Array Slice Notation) also sufficiently solve this problem, and are advancing quicker through the standards track. Should one of these proposals advance to Stage 3, this proposal will be dropped.
Rationale
Currently the only way to get the last element of an Array is to subtract one from the .length property and use this value in the property lookup. Typically this looks like someArray[someArray.length - 1].
For such a common operation, it is easy to forget to -1 from the length property, or to overcook it, and as such there should be a simpler syntax for doing this.
High Level Proposal
Array.prototype.lastItem is a property with a getter/setter function that returns the last item of the Array. Array.prototype.lastIndex is a property with a getter function that returns the last index of the Array.
Specification
Polyfill
A polyfill is available in the core-js library. You can find it in the ECMAScript proposals section.
It's a polyfill with usage abstract ECMAScript operations:
Other considered options
Array.prototype.last/Array.prototype.last()was originally proposed but has web-compatibility issues.Array.prototype.end()as a method. The downside being that setting the last element of an array is still uses awkward syntax.Array.prototype.endas a getter. HoweverlastItemwas a more popular name choice.Array.prototype.peek()was considered (marries well withpush,pop) butpeekas a setter is unintuitive.Array.prototype.prePop()was mentioned but suffers from the same issues aspeek- it is unintuitive for setting, and potentially surprising if it doesn't mutate.- A bunch of other names. Most importantly see the following rational for a naming rubric:
- Not have webcompat issues
- Within the top 1000 most popular english words (both last and end are)
- Should be intuitive for both getting and setting
- Ideally not be a compound word (like lastItem)