get last element of array javascript slice code example

Example 1: js take last item in array

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

Example 2: how to get the last eleemnt of an array

var Cars = ["Volvo", "Mazda", "Lamborghini", "Maserati"];
//We can get the total number of elements like this.
var hmCars = Cars.pop();
//hmCars is now Maserati
console.log(hmCars)

Example 3: javascipt get last element of array

const arr = [1,2,3] ;
console.log(arr[arr.length - 1]);

Example 4: how to get the last element of an array

//Lets we have a array called arr
let arr = ["s","fg","d"]
//Lets print the last element
print(arr[arr.length-1])