check if last element in array javascript code example
Example 1: find last element in array javascript
var heroes = ["Batman", "Superman", "Hulk"];
var lastHero = heroes.pop();
Example 2: find last element in array javascript
const numArray = [1,2,3,4,5];
const lastNumber = numArray.reverse()[0];
Example 3: how to check the last item in an array javascript
const colors = ['red', 'yellow', 'green', 'blue']
const lastItem = colors[colors.length - 1]
console.log(lastItem)
Example 4: js detect end of array
var group = ["a","b","c","d"];
var groupLength = group.length;
while (groupLength--) {
var item = group[groupLength];
if(groupLength == 0){
console.log("Last iteration with item : " + item);
}
}
Example 5: js detect end of array
var group = ["a","b","c","d"];
var groupLength = group.length;
for(var i = 0;i < groupLength;i++){
var item = group[i];
if((i + 1) == (groupLength)){
console.log("Last iteration with item : " + item);
}
}