Get the last item in an array

Use Array.pop:

var lastItem = anArray.pop();

Important : This returns the last element and removes it from the array


Not sure if there's a drawback, but this seems quite concise:

arr.slice(-1)[0] 

or

arr.slice(-1).pop()

Both will return undefined if the array is empty.


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
}