how to get the last element of an array in js code example

Example 1: javascript get last element of array

var foods = ["kiwi","apple","banana"];
var banana = foods[foods.length - 1]; // Getting last element

Example 2: javascript last element of array

let arr = ['a', 'b', 'c']
arr.slice(-1)[0] // returns last element in an array

Example 3: 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 4: get last element of array javascript

var numbers = ["one", "two", "three"];
var lastnumber = numbers[numbers.length - 1];
console.log(lastnumber);

Example 5: js get last array element

const array = [1, 2, 3, 4]
const lastArrayElement = array[array.length - 1]

Example 6: javascript get last element of array

console.log(2)