TC39 proposal: Array.lastItem
Tags: javascript, tc39, language
Published: 2019-03-28 20:46:57

---
title: TC39 proposal: Array.lastItem
published: true
description: A recent TC39 JavaScript-language proposal offers easier access to terminal elements with a new property, Array.lastItem
tags: JavaScript, TC39, language
---

A [recent TC39 proposal](https://github.com/keithamus/proposal-array-last) offers easier access to the last element in a JavaScript Array. While on the surface this does not seem significant, there are [many benefits](https://medium.freecodecamp.org/the-first-shall-be-last-with-javascript-arrays-11172fe9c1e0) to such a proposal.

Terminal elements are unique in that they tend to be accessed more often than middle elements. Because of this, many other scripting languages like [Ruby](https://stackoverflow.com/questions/18212240/ruby-convention-for-accessing-first-last-element-in-array) and [Python](http://knowledgehills.com/python/negative-indexing-slicing-stepping-comparing-lists.htm) provide convenience methods and properties to access and modify them.

The only way to do this in ES6 is with indices. But even a trivial task like swapping the last elements in two arrays quickly becomes difficult to read.

{% gist https://gist.github.com/Tombarr/3a4c1e75722162f69c2bfd27e4b18920 %}

The most common solution today is to use libraries like [Lodash](https://lodash.com/) or [Underscore](https://underscorejs.org/) that include utility functions for accessing the last element. This offers an improvement in clarity, but introduces several complications. Most obviously, they do not include a way to _set_ the last element. Even if they did, doing so would add ambiguity by removing the `=` assignment operator.

{% gist https://gist.github.com/Tombarr/56846a6932bc9e32d2c0b6da73ab7afc %}

That is where the ECMAScript Technical Candidate (TC39) proposal comes in. In its current form, it introduces two new properties: `Array.lastItem` and `Array.lastIndex`. The first property is especially useful because it works both for setting and getting the last item.

{% gist https://gist.github.com/Tombarr/ae41f81f914ab622dc4b029a6875b250 %}

This proposal is actually [already supported](https://kangax.github.io/compat-table/esnext/) in [core-js 3](https://github.com/zloirock/core-js) and can be used with Babel and TypeScript today. However, I do not have much use for `Array.lastIndex` and would prefer a symmetrical for the first element. For prototypes and smaller projects I wrote two simple, dependency-free properties that do just that.

{% gist https://gist.github.com/Tombarr/70683bab8e2219740ed1cb5378737e43 %}


I recently learned that destructuring in ES6 can [swap array elements without temporary variables](https://medium.com/@frontman/how-swap-two-values-without-temporary-variables-using-javascript-8bb28f96b5f6). This is a great example of where `Array.first` and `Array.last` can really improve clarity.

{% gist https://gist.github.com/Tombarr/defaf40c4b929c795f4c240e884f9f8a %}

Suggestions of properties like `Array.first` and `Array.last` go [back to at least 2013](https://esdiscuss.org/topic/array-prototype-last). Hopefully this [Stage 1 proposal](https://github.com/tc39/proposals#stage-1) continues to get traction and will be coming to a version of JavaScript near you soon.