All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Get Last Element Of Array JavaScript

Last Updated : Mar 11, 2024

Get Last Element Of Array JavaScript

In this tutorial we will show you the solution of get last element of array JavaScript using several methods, we recommend that you read the full article if you are new to programming or JavaScript.

You can skip ahead to the code part below if you're only interested in the code.

A container object that stores a number of values of a single type is called an array.

The length of an array would remain constant/fixed once it was created. Let's identify the last element in an array now that we have a fundamental understanding of what an array is.

Step By Step Guide On Get Last Element Of Array JavaScript :-

1. Array length property

<html>
<head>
<title></title>
</head>
<body>
<script>
let arry = [2, 10, 12, 14, 16];
let lastElement = arry[arry.length - 1];
console.log(lastElement);
</script>
</body>
</html>
  1. Number of elements in an array is returned via the length attribute. You can Subtract 1 from the length of an array which returns the index of the array's last element, which can be used to access the last element.
  2. Because array index numbering in JavaScript starts with 0, we're subtracting 1 from the length. i.e. the index of the element which is first would be 0. As a result, the index of the last element would equal array length -1.

2. Using the slice() method

<html>
<head>
<title></title>
</head>
<body>
<script>
let arry = [2, 10, 12, 14, 16];
let lastElement = arry.slice(-1);
console.log(lastElement);
</script>
</body>
</html>
  1. This method returns a new array object containing particular elements from an array. This method picks elements beginning at the specified start index and ending at the specified end index, except the element at the specified end index.
  2. The array is not modified by the slice() method. A positive index value calculates the index from the beginning of the array, while a negative index value counts the index from the end of the array.

3. Using pop() method

<html>
<head>
<title></title>
</head>
<body>
<script>
var myList = ["Peter", "Smith", 25, 23, "Random String", 10]
var lastElement = myList.pop(); console.log(lastElement) console.log(myList)
</script>
</body>
</html>
  1. The pop() method returns the last element while also removing it. However, we may prevent this functionality by temporarily copying the list in memory and calling pop() on the copy, keeping the original list intact, if we utilise the spread operator from ES6.
  2. If you do not need the original list to stay intact, you can pop elements from it; however, if you do, you can make a copy and pop() from that.

Conclusion :-

If you want to get the last item from an array, you can use the length property of a JavaScript array, as well as its slice and pop methods.

The fastest technique is pop(). If you don't mind changing the array, you can use it. The slice() method can be used if you don't want to modify the array.

I hope this tutorial on get last element of array JavaScript helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Amruta

Amruta is an Experienced web developer with 4 years for experience she completed her master's with MCA and passionate about programming Languages for creating technical contents like HTML, CSS, JavaScript, Java, Python, PHP, jQuery.

Follow Amruta On Linkedin 🡪