How To Get the last item in an array in JavaScript

Arrays can be used to store multiple values in a variable. So 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 array.pop() method.

In this tutorial, you’ll learn about the different methods to get the last item in an array.

If You’re in Hurry…

You can use the code snippet to get the last item in an array in javascript.

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

    let lastItem = birds.pop();

    console.log(lastItem);

Output

    "cuckoos"

If You Want to Understand Details, Read on…

Using Array.Pop()

Array.pop() is used to remove the last item of an array and returns the removed item. Also, this will change the length of the array.
Syntax

    array.pop()

Parameters
– this method does not accept parameters
return value
– removes the last item of an array and return that value.
– returns undefined if array is empty.
Code

    const birds = ["dove", "parrot", "finch", "owl", "cuckoos"];
    const bird = birds.pop(); 
    console.log(bird); // return the last item that is removed

Output

    "cuckoos"

Using Array.Slice()

Array.slice() will return a shallow portion of an array into an new array object based on the start and end index position. array.slice() will not modify the original array.
Syntax

    slice()
    slice(start)
    slice(start, end)

Parameters
start
Index position to start copy
– if negative index is used, offset will be set from the end of the sequence and copy values from the end of the array. slice(-1), copies the last value of the array.
– if start is undefined, the index is set to 0
– if start is greater than array length, an empty array returned
end
index position to end copy
– if end is not specified, the slice will copy till the end of the array based on the array.length.
– if end is greater than the size of the array, the slice will copy till the end of the array based on the array.length.
– if end is specified with negative index, offset is set from the end of the sequence.
return value
– returns a new array based on the parameters.
Code

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

    let lastItem = birds.slice(-1);

    console.log(lastItem);

Output

    ["cuckoos"]

To get the last item in an array using pop(), you need to pass the start value as -1, so that offset will be set from the end of the array, and the last one item will be returned.

Using Array.Length()

Using array length is the traditional way to get the last item in an array. In this, you will use one less the array’s length as the index position to get the last item in an array.
The reason for subtracting one from the array’s length is that the array index starts from “0“.
Code

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

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

    console.log(lastItem);

Output

    "cuckoos"

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.

Using Array. At()

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

    at(index)

Parameters
index
Index position of the item to be returned.
– if negative index is used, offset will be set from the end of the array and count from back to front.
return value
– returns matching items in the specified index. Returns undefined if the specified index is not found.
Code

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

    let lastItem = birds.at(-1);

    console.log(lastItem);

Output

    "cuckoos"

In the above examples, you can see 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.
As a result, the last item of the array is returned.

Using Array.reverse()

Using Array.reverse() is not a straightforward way and is also an expensive way to get the last item of an array.
First, the array has to 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.

Conclusion

In this tutorial, you’ve learned how to get the last item in an array in Javascript. You’ve also known different ways to get the last item in an array.

If you’ve any questions, please comment below.

You May Also Like

Leave a Comment