To get the last element of an array in JavaScript, you can use several methods:

  • array[array.length - 1]: The most straightforward and widely supported method. It accesses the element at the index equal to the array length minus one.

  • array.slice(-1): Returns a new array containing only the last element. Use [0] to get the value: array.slice(-1)[0].

  • array.pop(): Returns and removes the last element from the array. Modifies the original array, so use only if you don’t need to preserve it.

  • array.at(-1): A modern, clean method introduced in ES2022. Pass -1 to access the last element directly: array.at(-1).

For non-destructive access, array.at(-1) is recommended for its clarity and safety. Use array.pop() only if you want to remove and retrieve the last element.

if (loc_array[loc_array.length - 1] === 'index.html') {
   // do something
} else {
   // something else
}

In the event that your server serves the same file for "index.html" and "inDEX.htML" you can also use: .toLowerCase().

Though, you might want to consider doing this server-side if possible: it will be cleaner and work for people without JS.


EDIT - ES-2022

Using ES-2022 Array.at(), the above may be written like this:

if (loc_array.at(-1) === 'index.html') {
   // do something
} else {
   // something else
}
Answer from Aaron Butacov on Stack Overflow
🌐
Reddit
reddit.com › r/javascript › the easy way to access the last javascript array element
r/javascript on Reddit: The easy way to access the last JavaScript array element
September 16, 2023 - .at(-1) for an array with three elements, the latter runs roughy twice as fast on my machine. This is a very simple benchmark though, and results will differ based on multiple factors. Changing existing JavaScript behaviour is indeed a no go, though. ... That test is only done on static array (no changes to it). I tested with the following codes: "list" as the random generated list of ten to twenty numbers · "result" as the collection of the last index of the random list
Discussions

javascript - Access last element of a TypeScript array - Stack Overflow
By the index of the last element. ... Use the slice function: this function returns the value in an array that's why accessing the 0'th index is required. ... Use the pop() function: this method is helpful when you need to get and remove the last element of the array. More on stackoverflow.com
🌐 stackoverflow.com
how to get the last element of an array?
For the second part of the challenge, you want to set numberOfSports to the length of the sports array. To do this, you should use the length() method off the sports array. For the final part of the challenge, you want to set lastSport to whatever is at index 0 in the sports array. More on teamtreehouse.com
🌐 teamtreehouse.com
6
August 19, 2015
The easy way to access the last JavaScript array element
There was a competing proposal to add arr.lastElement and I think arr.lastIndex. But it was abandoned in favor of arr.at(). More on reddit.com
🌐 r/javascript
46
19
September 16, 2023
How to get the last item of an array with destructuring

Why not just do array[array.lemth -1]

More on reddit.com
🌐 r/javascript
22
6
April 23, 2019
🌐
Built In
builtin.com › articles › javascript-get-last-element-of-array
How to Get the Last Element of an Array in JavaScript | Built In
Summary: JavaScript offers multiple ways to get the last element of an array, including length - 1, slice(-1), pop(), at(-1) and more. Some methods mutate the original array, while others preserve it, making method choice dependent on use case. ...
🌐
Flexiple
flexiple.com › javascript › get-last-array-element-javascript
How to Get Last Element in an Array in JavaScript? - Flexiple
Get the last element of an array in JavaScript by using the length property of the array and subtract 1 from it.
🌐
W3Schools
w3schools.com › jsref › jsref_lastindexof_array.asp
JavaScript Array lastIndexOf() Method
Array[ ] Array( ) at() concat() constructor copyWithin() entries() every() fill() filter() find() findIndex() findLast() findLastIndex() flat() flatMap() forEach() from() includes() indexOf() isArray() join() keys() lastIndexOf() length map() of() pop() prototype push() reduce() reduceRight() rest (...) reverse() shift() slice() some() sort() splice() spread (...) toReversed() toSorted() toSpliced() toString() unshift() values() valueOf() with() JS Boolean
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array
Array - JavaScript | MDN
2 weeks ago - Returns the index of the first element in the array that satisfies the provided testing function, or -1 if no appropriate element was found. ... Returns the value of the last element in the array that satisfies the provided testing function, or undefined if no appropriate element is found.
Find elsewhere
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › findLast
Array.prototype.findLast() - JavaScript | MDN
January 28, 2026 - It calls a provided callbackFn function once for each element in an array in descending-index order, until callbackFn returns a truthy value. findLast() then returns that element and stops iterating through the array. If callbackFn never returns a truthy value, findLast() returns undefined.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › lastIndexOf
Array.prototype.lastIndexOf() - JavaScript | MDN
July 10, 2025 - The lastIndexOf() method of Array instances returns the last index at which a given element can be found in the array, or -1 if it is not present. The array is searched backwards, starting at fromIndex.
🌐
CoreUI
coreui.io › blog › how-to-get-the-last-element-in-an-array-in-javascript
How to get the last element in an array in JavaScript · CoreUI
January 25, 2024 - One such handy trick is retrieving the last element of an array—an operation that might seem straightforward but comes with nuances worth understanding. In this deep dive into JavaScript arrays, we’ll explore multiple ways to get to that elusive last item and offer insights into which method ...
Top answer
1 of 6
2
Hey Jorge! I'll try to explain each part of the challenge in an easier way to understand, sometimes the wording can confuse students: For the first part of the challenge, you are wanted to set bestSport to whatever is at index 0 in the sports array. Note that you will need to do this after the bestSport variable has been initialized. For the second part of the challenge, you want to set numberOfSports to the length of the sports array. To do this, you should use the length() method off the sports array. For the final part of the challenge, you want to set lastSport to whatever is at index 0 in the sports array. To do this, you should use the numberOfSports variable. This will give you a number starting from 1, of how long the array is. As we're working in indexes, we want to start from 0 and we can do this by taking 1 away from numberOfSports. Note that in this challenge, there isn't any specific ruling and in fact, you could cheat a bit and just set the bestSport variable to "Basketball", like you originally did. This will still pass the challenge but it's best practise to try and do it the proper way ;) Hope it helps and good luck with the challenge! If you get stuck along the way or if there's something you want clarified, feel free to give me a shout :)
2 of 6
2
Oh I didn't actually check the challenge, sorry. Hmm I have no idea where that soccer came from. But with this code I passed the challenge without any problem: ```java String[] sports = { "Basketball", "Baseball", "Tennis" }; String bestSport = sports[0]; int numberOfSports = sports.length; String lastSport = sports[numberOfSports - 1]; ```
🌐
Ignace Maes
blog.ignacemaes.com › the-easy-way-to-access-the-last-javascript-array-element
The easy way to access the last JavaScript array element | Ignace Maes - Blog
March 29, 2024 - Instead, you have to use brackets with the length of the array minus one. const frameworks = ['Nuxt', 'Remix', 'SvelteKit', 'Ember']; // ❌ Doesn't work frameworks[-1]; // undefined // ✅ Works frameworks[frameworks.length - 1]; // 'Ember' ...
🌐
4Geeks
4geeks.com › how-to › javascript-array-last-element
How to get the last element of an Array in Javascript?
July 16, 2025 - The previous code shows how to get the last element of an array by using the array's length, we are using the length property to determine the position of the last element, because the length of the array may vary so you can't use a fixed number, and since the array count always starts at 0, we can pick the last item by referencing the Array.length - 1 item.
🌐
TutorialsPoint
tutorialspoint.com › how-to-get-the-last-element-in-javascript-array
How to get the last element in JavaScript array?
Users can follow the syntax below to get the length using template literal. ... In the above syntax, we use the array.slice() method and got the last element using negative indexing (-1). In the below example, we apply the slice() method to find the last method in a JavaScript array.
🌐
CSS-Tricks
css-tricks.com › snippets › javascript › getting-first-and-last-items-in-array-and-splitting-all-the-rest
Getting First and Last Items in Array (and splitting all the rest) | CSS-Tricks
March 16, 2020 - I remember that .pop() is for snagging the last item from the array, like this: const arr = ["This", "Little", "Piggy"]; const first = arr.pop(); console.log(first); // "Piggy" So what’s the opposite of .pop()?
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › get-the-first-and-last-item-in-an-array-using-javascript
JavaScript - Get first and last Item of JS Array - GeeksforGeeks
July 11, 2025 - The array length property in JavaScript is used to set or return the number of elements in an array. ... The Array.pop() method removes the last element of the array and returns it and the Array.shift() method removes the first element from ...
🌐
Stack Abuse
stackabuse.com › javascript-get-last-element-in-list
JavaScript: Get Last Element in List
April 28, 2023 - We can access an element, based on its index, by passing it in square brackets [], referencing the array. The last element has the index of length-1. Since the length field just returns the saved value of elements - this is a fast, efficient and cheap operation.
🌐
Medium
medium.com › @iamdarius › 4-ways-to-remove-the-last-element-from-an-array-in-javascript-17749b12be0c
Learn 4 Ways to Remove the Last Element from an Array in JavaScript | by Darius Moore | Medium
September 8, 2022 - Lastly, the most common array method for removing elements is pop(). The popmethod removes the last element of the array, decrements the length, and returns the element that was removed. It’s important to note that this method modifies the original array that it is invoked on. ... That just about wraps it up. I hope you have learned a few additional ways to remove the last item from an array, without having to rely on just one approach. Stay tuned for more articles on JavaScript!
🌐
Maya Shavin
mayashavin.com › articles › find-last-element-array
Find the last matched element - the bad, the good, and the better
May 7, 2024 - Hence, to find our last matched element in the items array, we can use the findLast() method as follows: ... And that's all it takes. Using findLast() is more concise and readable than the previous approach, while maintaining the same efficiency.
🌐
W3Schools
w3schools.com › jsref › jsref_array_findlast.asp
JavaScript Array findLast() Method
const ages = [3, 10, 18, 20]; function ... = ages.findLast(checkAge); } Try it Yourself » · The findLast() method returns the value of the last element that passes a test....