To get the last element of an array, firstly we need to identify the length of the array. We can easily get the length of an array by using the built-in attribute function called length
//we declare an array variable var foods = ['burger', 'fried rice', 'pasta', 'chips', 'bread'];
To get the length of the array, we use the attribute length.
var arrayLength = foods.length;
As you know the starting index of an array is from index 0. So what we need to do is to return the total length of the array and minus it by 1. For example, if we want to get the first element of the array object, we can use the following code.
//This will return a value of burger. var firstObject = foods;
So to get the last element of an array, we can use the following code.
var lastObject = foods[foods.length - 1];