How to get the last element of an Array in Javascript

So you have a Javascript array, and you want to get the last element. Take this example, for instance:

let myArray = [ ‘๐Ÿ”ฉ’, ‘โšก๏ธ’, ‘๐Ÿ”‘’, ‘๐Ÿ–‡’ ]

This array has 4 items – and as you might know, to get any element within it, we can use the square br…


This content originally appeared on DEV Community ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป and was authored by Johnny Simpson

So you have a Javascript array, and you want to get the last element. Take this example, for instance:

let myArray = [ '๐Ÿ”ฉ', 'โšก๏ธ', '๐Ÿ”‘', '๐Ÿ–‡' ]

This array has 4 items - and as you might know, to get any element within it, we can use the square bracket notation []. For example, to get the lighting bolt, we can use myArray[1]:

let myArray = [ '๐Ÿ”ฉ', 'โšก๏ธ', '๐Ÿ”‘', '๐Ÿ–‡' ]
console.log(myArray[1]) // โšก๏ธ

Since arrays start at an index of 0, the first element is actually myArray[0], and so on. So to get the last element of the array, we can use myArray.length - 1. This gets the length of the array (4), and subtracts 1, to take into consideration that arrays start counting at 0. Therefore, to get the last element of an array using the square bracket notation, we can do something like this:

let myArray = [ '๐Ÿ”ฉ', 'โšก๏ธ', '๐Ÿ”‘', '๐Ÿ–‡' ]
console.log(myArray[myArray.length-1]) // ๐Ÿ–‡

This is the most common way to get the last element of an array. However, you can also use the at method to do the same thing:

let myArray = [ '๐Ÿ”ฉ', 'โšก๏ธ', '๐Ÿ”‘', '๐Ÿ–‡' ]
console.log(myArray.at(myArray.length-1)) // ๐Ÿ–‡

And even better, you can simply write myArray.at(-1)! This greatly simplifies getting the last element of an array to a simple expression:

let myArray = [ '๐Ÿ”ฉ', 'โšก๏ธ', '๐Ÿ”‘', '๐Ÿ–‡' ]
console.log(myArray.at(-1)) // ๐Ÿ–‡

As you might expect, at starts counting backwards, so at(-2) will return the key:

let myArray = [ '๐Ÿ”ฉ', 'โšก๏ธ', '๐Ÿ”‘', '๐Ÿ–‡' ]
console.log(myArray.at(-2)) // ๐Ÿ”‘


This content originally appeared on DEV Community ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป and was authored by Johnny Simpson


Print Share Comment Cite Upload Translate Updates
APA

Johnny Simpson | Sciencx (2022-10-15T16:39:11+00:00) How to get the last element of an Array in Javascript. Retrieved from https://www.scien.cx/2022/10/15/how-to-get-the-last-element-of-an-array-in-javascript/

MLA
" » How to get the last element of an Array in Javascript." Johnny Simpson | Sciencx - Saturday October 15, 2022, https://www.scien.cx/2022/10/15/how-to-get-the-last-element-of-an-array-in-javascript/
HARVARD
Johnny Simpson | Sciencx Saturday October 15, 2022 » How to get the last element of an Array in Javascript., viewed ,<https://www.scien.cx/2022/10/15/how-to-get-the-last-element-of-an-array-in-javascript/>
VANCOUVER
Johnny Simpson | Sciencx - » How to get the last element of an Array in Javascript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/10/15/how-to-get-the-last-element-of-an-array-in-javascript/
CHICAGO
" » How to get the last element of an Array in Javascript." Johnny Simpson | Sciencx - Accessed . https://www.scien.cx/2022/10/15/how-to-get-the-last-element-of-an-array-in-javascript/
IEEE
" » How to get the last element of an Array in Javascript." Johnny Simpson | Sciencx [Online]. Available: https://www.scien.cx/2022/10/15/how-to-get-the-last-element-of-an-array-in-javascript/. [Accessed: ]
rf:citation
» How to get the last element of an Array in Javascript | Johnny Simpson | Sciencx | https://www.scien.cx/2022/10/15/how-to-get-the-last-element-of-an-array-in-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.