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 21, 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 21, 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 28, 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. ...
🌐
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
🌐
Flexiple
flexiple.com › javascript › get-last-array-element-javascript
How to Get Last Element in an Array in JavaScript? - Flexiple
March 14, 2022 - Get the last element of an array in JavaScript by using the length property of the array and subtract 1 from it.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array
Array - JavaScript | MDN
5 days 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 › lastIndexOf
Array.prototype.lastIndexOf() - JavaScript | MDN
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.
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' ...
🌐
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.
🌐
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.
🌐
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 ...
🌐
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()?
🌐
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!
🌐
W3Schools
w3schools.com › jsref › jsref_array_findlast.asp
JavaScript Array findLast() Method
array.findLast(function(currentValue, index, arr),thisValue) Find the value of the last element with a value above a specific number: <p><input type="number" id="ageToCheck" value="18"></p> <button onclick="myFunction()">Try it</button> <p ...
🌐
Stackademic
blog.stackademic.com › quick-way-to-get-last-item-from-javascript-array-26e259f208b1
A Quick Way to Get the Last Item from an Array in JavaScript | by Fang Jin | Stackademic
December 11, 2023 - Therefore to take care of the above ... simply replace it with: ... Array.prototype.at2 = function (i, emptyFn) { const last = this.at(i) if (last === undefined) { return emptyFn(i) } return last }...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › findLastIndex
Array.prototype.findLastIndex() - JavaScript | MDN
July 20, 2025 - The findLastIndex() method of Array instances iterates the array in reverse order and returns the index of the first element that satisfies the provided testing function. If no elements satisfy the testing function, -1 is returned. See also the findLast() method, which returns the value of ...
🌐
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 ...
🌐
Zipy
zipy.ai › blog › get-the-last-item-in-an-array-using-javascript
get the last item in an array using javascript
April 12, 2024 - This operation, though seemingly straightforward, is a cornerstone of array manipulation and plays a crucial role in data handling, algorithm implementation, and UI development. This article dives into various methods to retrieve the last element of an array in JavaScript, catering to developers aiming to refine their coding skills with simple yet effective techniques.