How do I get the last item in an array?

Hi!

const bobsFollowers = ['Jane', 'Izumi', 'Sasha', 'Natsu'];
const tinasFollowers = ['Haru', 'Izumi', 'Natsu'];

const mutualFollowers = [];

for(var i=0; i<bobsFollowers.length; i++){
  for(var j=0; j<tinasFollowers.length; j++){
    if(bobsFollowers[i] === tinasFollowers[j]){
      mutualFollowers.push(bobsFollowers[i]);
      console.log('i and j are ' + bobsFollowers[i]);
      console.log('The mutual Followers are ' + mutualFollowers);
      console.log('The last mutual Follower is ' + mutualFollowers[mutualFollowers.length-1]);
      
    }
    else{
      console.log('i is '+ bobsFollowers[i]+ ' and j is '+ tinasFollowers[j]);
    }
  }
  
}

Is there a way to get the value of an array index without using this trick?

console.log('The last mutual Follower is ' + mutualFollowers[mutualFollowers.length-1]);

Thank you!

That looks to be the best way to get the last item and the most dynamic. Its even in the documentation

The Array.slice() method is ideal for this purpose…

arr = [1,5,9,2,6,4,8,7]
console.log(arr.slice(-1))    // 7
3 Likes