Nayuki
0
Q:

get last element of array javascript

var foods = ["kiwi","apple","banana"];
var banana = foods[foods.length - 1]; // Getting last element
37
var colors = ["red","blue","green"];
var green = colors[colors.length - 1];//get last item in the array
50
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 array =[1,2,3,4];
var last= array[array.length-1];
1
var colors = ["black", "white", "red", "yellow"];
var yellow = colors[colors.length - 1];
1
this = array[array.length - 1];
1
var last = arr.slice(-1)[0]
1

New to Communities?

Join the community