Answers for "how to identified last child in array"

26

js take last item in array

const heroes = ["Batman", "Superman", "Hulk"];
const lastHero = heroes.pop(); // Returns last elment of the Array
// lastHero = "Hulk"
Posted by: Guest on March-16-2020
0

how to find last element in array in javascript

// how to find last element in array in javascript
// 1. Array.prototype.length
const arr = [1,2,3,4,5];
console.log(arr[arr.length - 1]);
// Result: 5

// 2. Array.prototype.slice()
const last = arr.slice(-1);
console.log(+last);
// Result: 5
Posted by: Guest on January-07-2022

Code answers related to "Javascript"

Browse Popular Code Answers by Language