javascript get last element of array
var foods = ["kiwi","apple","banana"];
var banana = foods[foods.length - 1]; // Getting last element
javascript get last element of array
var foods = ["kiwi","apple","banana"];
var banana = foods[foods.length - 1]; // Getting last element
js get last element of array
var last_element = my_array[my_array.length - 1];
getting array last element fetching
let arry = [2, 4, 6, 8, 10, 12, 14, 16];
console.time('array length property');
let lastElement = arry[arry.length - 1];
console.log(lastElement);
console.timeEnd('array length property');
console.time('array slice method');
let lastElement1 = arry.slice(-1);
console.log(lastElement1);
console.timeEnd('array slice method');
console.time('array pop method');
let lastElement2 = arry.pop();
console.log(lastElement2);
console.timeEnd('array pop method');
//Output:
//16
//array length property: 13.798ms
//[ 16 ]
//array slice method: 8.839ms
//16
//array pop method: 0.138ms
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us