How To Get the last item in an array in JavaScript – With Examples

Arrays can be used to store multiple values in a single variable. When you work with Arrays, you may require to get the last item in an array in JavaScript.

You can get the last item in an array in JavaScript using the array.pop() method.

Basic Example

    var birds = ["dove", "parrot", "peacock", "finch", "cuckoos"];

    let lastItem = birds.pop();

    console.log(lastItem);

Output

    "cuckoos"

This tutorial teaches you the different methods to get the last item of an array in JavaScript and when it is appropriate to use each method.

Get the last item of the Array Using Array.Pop()

Array.pop() is used to remove the last item of an array and returns the removed item.

  • The pop method will change the length of the array.

You can use this method when you want to remove the last item from the array and return it.

Code

    const birds = ["dove", "parrot", "finch", "owl", "cuckoos"];

    const bird = birds.pop(); 

    console.log(bird); // return the last item that is removed

Output

    "cuckoos"

Get the last item of the Array Using Array.Slice()

The Array.slice() returns a shallow portion of an array into a new array object based on the start and end index position.

To get the last item as an array using the slice() method,

  • pass -1 to the slice() method.
  • array.slice() will not modify the original array.

You can use this method when you want to get the last item of the array as a separate array.

Code

    var birds = ["dove", "parrot", "peacock", "finch", "cuckoos"];

    let lastItem = birds.slice(-1);

    console.log(lastItem);

Output

    ["cuckoos"]

Get the last item of the Array Using Array.Length()

Using array length is the traditional way to get the last item in an array.

  • Use one value less than the array length to get the last item of the array because the index is 0-based.

You can use this method when you want to get the last item of the array without modifying the array.

Code

    var birds = ["dove", "parrot", "peacock", "finch", "cuckoos"];

    let lastItem = birds[birds.length - 1];

    console.log(lastItem);

Output

In the above example, the length of the array(5) -1 [5-1 = 4] is the index position to get the last item. Value at 4th index position(“cuckoos”) is returned, which is the last item of an array.

    "cuckoos"

Get the last item of the Array Using Array. At()

Array.at() takes integer(positive or negative) as index position and return value present in the index position.

You can use this method when you want to get the last item of the array without modifying the array

Code

    var birds = ["dove", "parrot", "peacock", "finch", "cuckoos"];

    let lastItem = birds.at(-1);

    console.log(lastItem);

Output

In the above examples, you can see a negative index position of -1 is given to array.at(-1), so the match will be taking place from the back to the front of the array.

    "cuckoos"

Get the last item of the Array Using Array.reverse()

You can use the array.reverse() to get the last item of the array in JavaScript.

However, using Array.reverse() is not a straightforward way, and it is also expensive to get the last item of an array.
While using the reverse() method, first, the array will be reversed using Array.reverse(), and by doing this, the first item of an array becomes the last item, and the last item of an array becomes the first.

After reverse, if you get the item in the first index, it will give you the last item of the array.

Code

    var birds = ["dove", "parrot", "peacock", "finch", "cuckoos"];

    let lastItem = birds.reverse()[0];

    console.log(lastItem);

Output

    "cuckoos"

JSfiddle

This tutorial is available in this JSFiddle.

Additional Resources

Leave a Comment