Get the last item in an array in Javascript

javascript last element in array es6
javascript get last n elements of array
get last element of array java
get last element of array jquery
get last element of array php
typescript get last element of array
lodash get last element of array
get last element of array python

Here is my JavaScript code so far:

var linkElement = document.getElementById("BackButton");
var loc_array = document.location.href.split('/');
var newT = document.createTextNode(unescape(capWords(loc_array[loc_array.length-2]))); 
linkElement.appendChild(newT);

Currently it takes the second to last item in the array from the URL. However I 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.


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.

Get the last item in an array, I find slice at -1 useful for getting the last element (especially of an array of unknown length) and the performance is much better than calculating the length less  Get the last element of an Array in JavaScript. This article discusses the preferred way to retrieve the last element of an Array in JavaScript. Tip 7 in this Useful JavaScript Hacks Article proposes the following method to get the last item in an array. var array = [1,2,3,4,5,6];


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.

Selecting last element in JavaScript array, Tip 7 in this Useful JavaScript Hacks Article proposes the following method to get the last item in an array. syntax1.js. 1. 2. var array = [  Get the first and last item in an array using JavaScript Javascript array is a variable which holds multiple values at a time. The first and last elements are accessed using an index and the first value is accessed using index 0 and the last element can be accessed through length property which has one more value than the highest array index.


Use Array.pop:

var lastItem = anArray.pop();

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

Get the last element of an Array in JavaScript, How can you get the last item in the array? In this case the array has 4 items. You know you can get the first item using colors[0] , the second  Get the last item in an array - 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.


A shorter version of what @chaiguy posted:

Array.prototype.last = function() {
    return this[this.length - 1];
}

Reading the -1 index returns undefined already.

EDIT:

These days the preference seems to be using modules and to avoid touching the prototype or using a global namespace.

export function last(array) {
    return array[array.length - 1];
}

How to get last element of an array in JavaScript?, Learn how to get n last element from the array in javascript. Different ways to get the last elements from the array with and without changing the  There are multiple ways through which we can get the last element from the array in javascript. Using pop() We can use the pop() of array which removes and returns the last element of the array.


Two options are:

var last = arr[arr.length - 1]

or

var last = arr.slice(-1)[0]

The former is faster, but the latter looks nicer

http://jsperf.com/slice-vs-length-1-arr

How to get the last element of the array in javascript, Need an easy way to get the last element of an array in javascript? Heres a few options: var myArray = [1,2,3,4,5,6]; // Fastest method, requires  Need an easy way to get the last element of an array in javascript? Heres a few options: var myArray = [1,2,3,4,5,6]; // Fastest method, requires the array is in a variable myArray[myArray.length - 1]; // Also very fast but it will remove the element from the array also, this may or may not matter in your case. myArray.pop(); // Slowest but very readable and doesn't require a variable myArray.slice(-1)[0]


3 Methods To Get The Last Element Of An Array In Javascript, Javascript array is a variable which holds multiple values at a time. The first and last elements are accessed using an index and the first value is accessed using  See the Pen JavaScript - Get the last element of an array- array-ex-4 by w3resource (@w3resource) on CodePen. Improve this sample solution and post your code through Disqus Previous: Write a JavaScript function to get the first element of an array. Passing a parameter 'n' will return the first 'n' elements of the array.


Get the first and last item in an array using JavaScript, The pop() method removes the last element from an array and returns that element. This method changes the length of the array. How do you get the last item in an array using JavaScript? The first thing you need to get is the index value of the last item in the array. That value can be obtained dynamically with this method: array[array.length - 1]. By using that method, you can get the index of the last value of an array even if you don't know the length beforehand.


Array.prototype.pop(), The recommended solution to get any element of an array is using the [] operator. To get the last element, simply pass its index to the [] operator as shown below:. To get the last item without knowing beforehand how many items it contains, you can use the length property to determine it, and since the array count starts at 0, you can pick the last item by referencing the <array>.length - 1 item.