# Array.getLast()

**URL:** <https://es.discourse.group/t/array-getlast/851>\
**Category:** 💡 Ideas\
**Tags:** proposal\
**Created:** [July 13, 2021, 7:58am UTC](https://es.discourse.group/t/array-getlast/851 "2021-07-13T07:58:32Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![IagoLast](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/iagolast/32/184_2.png) [@IagoLast](https://es.discourse.group/u/IagoLast)\
**Post date:** [July 13, 2021, 7:58am UTC](https://es.discourse.group/t/array-getlast/851/1 "2021-07-13T07:58:32Z")

</div>

This is a very common use case. I often find myself doing the length - 1 trick:

```javascript
const myArrayWithLongDescriptiveName = ['red', 'yellow', 'green', 'blue']
const lastItem = myArrayWithLongDescriptiveName[myArrayWithLongDescriptiveName.length - 1]

```

**Proposal**

Like [Array.pop](https://developer.mozilla.org/es/docs/Web/JavaScript/Reference/Global_Objects/Array/pop) without mutating the original object

```javascript
const myArrayWithLongDescriptiveName = ['red', 'yellow', 'green', 'blue']
const lastItem = myArrayWithLongDescriptiveName.getLast() // blue

```

For symmetry I would also add **Array.getFirst** equivalent to a non-mutating [shift](https://developer.mozilla.org/es/docs/Web/JavaScript/Reference/Global_Objects/Array/shift)

* * *

Thoughs ?

---

<div class="post-metadata">

**Author:** ![jithujoshyjy](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/jithujoshyjy/32/399_2.png) [@jithujoshyjy](https://es.discourse.group/u/jithujoshyjy)\
**Post date:** [July 13, 2021, 8:37am UTC](https://es.discourse.group/t/array-getlast/851/2 "2021-07-13T08:37:45Z")

</div>

This seem to work for me!:

```javascript
Object.defineProperty(Array.prototype, "first", {
	get() {
		return this[0]
	},
	set(item) {
		return this[0] = item
	}
})

Object.defineProperty(Array.prototype, "last", {
	get() {
		return this[this.length -1]
	},
	set(item) {
		return this[this.length -1] = item
	}
})

const arr = [1, 2, 3, 4]
arr.first = 100
console.log("first: ", arr.first, ", last: ", arr.last)
// first: 100, last: 4

```

---

<div class="post-metadata">

**Author:** ![theScottyJam](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/thescottyjam/32/616_2.png) [@theScottyJam](https://es.discourse.group/u/theScottyJam)\
**Post date:** [July 13, 2021, 8:38am UTC](https://es.discourse.group/t/array-getlast/851/3 "2021-07-13T08:38:17Z")

</div>

There was a proposal to introduce yourArray.last (see [here](https://github.com/tc39/proposal-array-last)), which was later superceded by the more general purpose yourArray.at() proposal.

> **[tc39/proposal-relative-indexing-method](https://github.com/tc39/proposal-relative-indexing-method)**
>
> A TC39 proposal to add an .at() method to all the basic indexable classes (Array, String, TypedArray) - tc39/proposal-relative-indexing-method

This let's you do this to get the last item: yourArray.at(-1)

---

<div class="post-metadata">

**Author:** ![aclaymore](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/aclaymore/32/501_2.png) [@aclaymore](https://es.discourse.group/u/aclaymore)\
**Post date:** [July 13, 2021, 6:49pm UTC](https://es.discourse.group/t/array-getlast/851/4 "2021-07-13T18:49:22Z")

</div>

And `.at(-1)` is available in Firefox from today!

> **[Firefox 90 for developers - Mozilla | MDN](https://developer.mozilla.org/en-US/docs/Mozilla/Firefox/Releases/90#javascript)**
>
> This article provides information about the changes in Firefox 90 that will affect developers. Firefox 90 is the current Beta version of Firefox, and will ship on July 13, 2021.

---

<div class="post-metadata">

**Author:** ![debadeep](https://avatars.discourse-cdn.com/v4/letter/d/439d5e/32.png) [@debadeep](https://es.discourse.group/u/debadeep)\
**Post date:** [August 1, 2021, 4:14pm UTC](https://es.discourse.group/t/array-getlast/851/5 "2021-08-01T16:14:32Z")

</div>

Just checked on Edge. `.at` appears to be available here too.

---

<div class="post-metadata">

**Author:** ![graphemecluster](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/graphemecluster/32/957_2.png) [@graphemecluster](https://es.discourse.group/u/graphemecluster)\
**Post date:** [January 9, 2022, 9:56am UTC](https://es.discourse.group/t/array-getlast/851/6 "2022-01-09T09:56:37Z")

</div>

The problem is, the `.at` method is not paired with a `.setAt` method.  
We still need to do `array[array.length - 1] = value` or `array.pop(); array.push(value)` to set the last item of the array. Only the dropped `.lastItem` proposal @theScottyJam mentioned solves the problem.

---

<div class="post-metadata">

**Author:** ![aclaymore](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/aclaymore/32/501_2.png) [@aclaymore](https://es.discourse.group/u/aclaymore)\
**Post date:** [January 9, 2022, 10:21am UTC](https://es.discourse.group/t/array-getlast/851/7 "2022-01-09T10:21:02Z")

</div>

You can also do `arr.splice(-1, 1, v)`, which is from the last item, delete 1 item, and insert `v`

---

<div class="post-metadata">

**Author:** ![graphemecluster](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/graphemecluster/32/957_2.png) [@graphemecluster](https://es.discourse.group/u/graphemecluster)\
**Post date:** [January 9, 2022, 11:32am UTC](https://es.discourse.group/t/array-getlast/851/8 "2022-01-09T11:32:13Z")

</div>

Yea I missed that, but I don’t think it’s the best solution as something like `array[array.length - 1] += value` cannot be done.

---

<div class="post-metadata">

**Author:** ![theScottyJam](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/thescottyjam/32/616_2.png) [@theScottyJam](https://es.discourse.group/u/theScottyJam)\
**Post date:** [January 9, 2022, 3:41pm UTC](https://es.discourse.group/t/array-getlast/851/9 "2022-01-09T15:41:23Z")

</div>

@graphemecluster - a `+=` operator wouldn't work with a `.lastItem()` function either.

---

<div class="post-metadata">

**Author:** ![graphemecluster](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/graphemecluster/32/957_2.png) [@graphemecluster](https://es.discourse.group/u/graphemecluster)\
**Post date:** [January 9, 2022, 7:41pm UTC](https://es.discourse.group/t/array-getlast/851/10 "2022-01-09T19:41:57Z")

</div>

@theScottyJam  
No, in the proposal you mentioned, `.lastItem` is a property with both getter and setter.

---

<div class="post-metadata">

**Author:** ![theScottyJam](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/thescottyjam/32/616_2.png) [@theScottyJam](https://es.discourse.group/u/theScottyJam)\
**Post date:** [January 9, 2022, 9:16pm UTC](https://es.discourse.group/t/array-getlast/851/11 "2022-01-09T21:16:51Z")

</div>

Oh, whoops, you're right. It's been a while since I looked at it.
