showing results for - "get last element of array javascript"
Aubree
06 Mar 2016
1// Method - 1 ([] operator)
2const arr = [5, 3, 2, 7, 8];
3const last = arr[arr.length - 1];
4console.log(last);
5/*
6    Output: 8
7*/
8
9// Method - 2 (Destructuring Assignment)
10const arr = [5, 3, 2, 7, 8];
11
12const [last] = arr.slice(-1);
13console.log(last);
14/*
15    Output: 8
16*/
17
18// Method - 3 (Array.prototype.pop())
19const arr = [5, 3, 2, 7, 8];
20
21const last = arr.slice(-1).pop();
22console.log(last);
23/*
24    Output: 8
25*/
26
27// Method - 4 (Underscore/Lodash Library)
28const _ = require("underscore");
29
30const arr = [5, 3, 2, 7, 8];
31const last = _.last(arr);
32console.log(last);
33/*
34    Output: 8
35*/
Selyan
10 Jul 2017
1// Method - 1 ([] operator)
2const arr = [5, 3, 2, 7, 8];
3const last = arr[arr.length - 1];
4console.log(last);
5/*
6    Output: 8
7*/
8
9// Method - 2 (Destructuring Assignment)
10const arr = [5, 3, 2, 7, 8];
Violeta
30 Aug 2017
1let colors = ["red", "yellow", "green"];
2let lastELement = colors[colors.length - 1]
Anis
14 Oct 2020
1if (loc_array[loc_array.length - 1] === 'index.html') {
2   // do something
3} else {
4   // something else
5}
6
Finnian
12 May 2018
1var languages = ["JS", "PHP", "JAVA"]
2var lastElement = languages[languages.length - 1]
3console.log(lastElement)
Mateo
13 Jan 2019
1if (loc_array[loc_array.length - 1] === 'index.html') {
2   // do something
3} else {
4   // something else
5}
queries leading to this page
get last element of array javascripthow to get last element in array javascriptget last element of array javascript get last element of array javascripttake last element of array javascriptget last element of array javascriptjs for of last itemget last element of array javascriptget last element of array javascriptget last element of array javascriptget last element of array javascriptget last element of array javascriptlast element of arrayget last element of array javascript get last array value javascriptget last element of array javascriptget last element of array javascript get last element of array javascriptget last element of array javascriptget last element in array javascriptget last element of array javascriptget last element of array javascriptget last element of array javascriptget the last object in arrayget last element of array javascriptget last element of array javascriptget last element of array javascript get last element of array javascriptget last element of array javascriptget last element of array javascript js array get last itemget last element of array javascript get last element of array javascriptget last element of array javascriptget last element of array javascriptarray last elementget last element of array javascriptget last element of array javascriptget last element of array javascriptget last element of array javascript get last element of array javascript how to get last value in arrayget last element of array javascripthow to find last index of array in javascriptget last element of array javascriptget last element of array javascriptget last element of array javascriptget last element of array javascript get last element of array javascriptget last element of array javascriptjavasript find last element in arrayget last element of array javascriptget last element of array javascriptget last element of array javascripttake last element from an array jsget last element of array javascriptget last element of array javascript get last element of array javascriptget last element of array javascript get last element of array javascriptarray javascript last elementget last element of array javascriptget last element of array javascriptget last value in array javascriptjavascript array last elementget last element of array javascriptget last element of array javascriptget last element of array javascriptget last element of array javascriptget last element of array javascripthow to always find the last item in an array in javascriptget last element of array javascriptget last element of array javascriptget last element of array javascriptget last element arrayget last element of array javascriptjavascript how to get last element of listget last element of array javascriptselect last value of array javascriptget last element of array javascriptjavascript last array elementget last element of array javascript print the last element in the array javascriptget last element of array javascriptjavascript latest item in arrayget last element of array javascriptget last element of array javascriptget last element of array javascriptget last array element javascriptget last element of array javascriptget last element of array javascriptget last element of array javascripthow to get last element of array in typescriptget last element of array javascriptprint last element of an arrey jaavsciptget last element of array javascriptget last element of array javascriptget last element of array javascriptget last element of array javascriptget last element of array javascriptget the last element of an array jsget last element of array javascriptget last element of array javascript get last item of list javascriptget last element of array javascriptget last element of array javascriptget last element of array javascriptget last element of array javascript get last element of array javascriptjs get last element in listjavascript es6 get last element of arrayget last element of array javascript find last index of array in javascriptget last element of array javascript get last index of array how to get last array element in javascriptget last element of array javascriptget last element of array javascriptget last element of array javascript get last element of array javascriptget last item of array javascriptjs get last element from arrayget last element of array javascriptget last element of array javascriptjavascript get last element of arraywrite a javascript function to get the last element of an arrayhow to get last element of a arrayget last element of array javascriptget last element of array javascript get last element of array javascriptget the last element of an array typescriptget last value of array javascripthow to find last element of an array in javascript 3fhow to get last item from array javascriptlast element array javascriptget last element of array javascriptjavascript last element of arrayget last element of array javascript get last element of array javascript get last element of array javascriptget last element of array javascript get last element of array javascriptget last index jshow to get last element of array in javascriptlast element value of array jsget last element of array javascriptget last element of array javascriptget last element of array javascripthow to get the last element of an array in javascriptget last element of array javascriptget last element of array javascriptget last element of array javascriptget last element of array javascriptjavascript find the last element in arrayget last element of arrayget last element of array javascripthow to get the last element of a list in jsget last element of array javascript get last element of array javascriptget last item in array javascriptget last element of array javascriptget last element of array javascripthow to get the last index of an arrayget last element of array javascripthget last elemmnt arrayget last element of array javascript get last element of array javascriptget last element of array javascriptget last position array jsget last element of array javascriptget last element of array javascript