javascript tutorial - [Solved-5 Solutions] Get the last item in an array - javascript - java script - javascript array
Problem:
Here is my JavaScript code so far:
Currently it takes the second to last item in the array from the URL. However we want to do a check for the last item in the array to be "index.html" and if so, grab the third to last item instead.
Solution 1:
In the event that our server serves the same file for "index.html" and "inDEX.htML" we can also use: .toLowerCase().
Solution 2:
Not sure if there's a drawback, but this seems quite concise:
Both will return undefined if the array is empty.
Solution 3:
- Use Array.pop:
Solution 4:
A shorter version of what @chaiguy posted:
Solution 5:
Two options are:
The former is faster, but the latter looks nicer