0
Q:

getting last element of array javascript

var numbers = ["one", "two", "three"];
var lastnumber = numbers[numbers.length - 1];
console.log(lastnumber);
10
arr.slice(-1)[0] 
4
var colors = ["red","blue","green"];
var green = colors[colors.length - 1]; //get last item in the array
8
arr.slice(-1)[0] 
1
var colors = ["black", "white", "red", "yellow"];
var yellow = colors[colors.length - 1];
1
let colors = ["red", "yellow", "green"];
let lastELement = colors[colors.length - 1]
1
if (loc_array[loc_array.length - 1] === 'index.html') {
   // do something
} else {
   // something else
}
1
const myArray = [1, 2, 3]

console.log(myArray.item(-1))
//=> 3
0

New to Communities?

Join the community