how to get last element from array in javascript code example

Example 1: javascript get last element of array

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

Example 2: how to get the end of an array javascript

let arr = [1, 2, 3, 4, 5, 6, 7, 8];
let last = arr.pop();

console.log(last);
//8


console.log("https://discord.gg/5yjWgMS")

Example 3: get last item in array javascript

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

Example 4: how to get the last element in javascript

const nums = [1, 2, 3, 4, 5, 6, 7];
const lastOne = nums[nums.lenght - 1]; // last element of array

Example 5: find last item in an array JS

if (loc_array[loc_array.length - 1] === 'index.html') {
   // do something
} else {
   // something else
}