last element in list js code example

Example 1: javascript last element of an array

var colors = ["red","blue","green"];
var green = colors[colors.length - 1];//get last

Example 2: find last element in array javascript

var heroes = ["Batman", "Superman", "Hulk"];
var lastHero = heroes.pop(); // Returns last elment of the Array
// lastHero = "Hulk"

Example 3: find last element in array javascript

const numArray = [1,2,3,4,5];
const lastNumber = numArray.reverse()[0];

Example 4: javascript array last element

var lastEle = arr.slice(-1)[0];
// or
var lastEle = arr.slice(-1).pop();