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*/
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];
1let colors = ["red", "yellow", "green"];
2let lastELement = colors[colors.length - 1]
1if (loc_array[loc_array.length - 1] === 'index.html') {
2 // do something
3} else {
4 // something else
5}
6
1var languages = ["JS", "PHP", "JAVA"]
2var lastElement = languages[languages.length - 1]
3console.log(lastElement)
1if (loc_array[loc_array.length - 1] === 'index.html') {
2 // do something
3} else {
4 // something else
5}