check if last element of array javascript code example

Example 1: find last element in array javascript

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

Example 2: find last element in array javascript

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

Example 3: how to check the last item in an array javascript

const colors = ['red', 'yellow', 'green', 'blue']
const lastItem = colors[colors.length - 1]
console.log(lastItem)

Example 4: how to find last element in array

int [] y={1,2,3,4,5};
int c=(n.length-1);