How to Get Last Element of array in Javascript?

TechDyer

Working with arrays is a recurring task in the JavaScript programming community. Regardless of experience level, accessing the final element of an array is frequently required. Thankfully, JavaScript offers several ways to achieve this, each with advantages and applications. We’ll look at several methods in this tutorial to Get last Element of array in Javascript.

What is an Array?

Let’s first define an array before moving on to how we can obtain the final element of an array. In JavaScript, an array is a data structure that has the ability to hold multiple values. Imagine it as a list or collection of items, with an index being the unique position for each item. The array’s first element has an index of 0, the second element has an index of 1, and so on since the index starts at 0.

Get Last Element of array in Javascript via length Property

Using the length property is one of the most popular methods for obtaining an array’s final element. The number of elements in the array is returned by the length property. The index of the final element is always one less than the length of the array since the index of the first element is 0.

Example

const lastElement = fruits[fruits.length – 1];

console.log(last element); // Output: ‘cherry’

See also  Python vs Java: Which is Better?

The fruit array in this example has a length of three. The index of the final element is 2 (3 – 1). Consequently, fruits[2] provide us with the final element, “cherry.”

Get Last Element of array in Javascript via the pop() method

Another method to obtain the last element of an array is to use the pop() method. The pop() function returns the array’s final element after removing it. This function removes the last element from the original array in addition to retrieving its last element.

Example:

const lastElement = fruits.pop();

console.log(last element); // Output: ‘cherry’

console.log(fruits); // Output: [‘apple’, ‘banana’]

The last element (‘cherry’) is returned by fruits. pop() in this example, and it is removed from the fruits array. Only ‘apple’ and ‘banana’ remain in the fruit array.

Get Last Element of array in Javascript via the slice() method

Specific elements from an array are returned as a new array object by the slice() method. Except for the element at the end index, this method chooses the elements beginning at the specified start index and ending at the specified end index. The current array is not changed by the slice() method. A negative index value is used to calculate the index from the end of the array, while a single index value returns the element at that position.

let arry = [2, 4, 6, 8, 10, 12, 14, 16];

let lastElement = arry.slice(-1);

console.log(lastElement);

//Output: 16

Performance

Now let’s apply all three techniques to an array to extract the final element and see which works the quickest.

See also  ReactJS vs React Native: A Comprehensive Analysis

let arry = [2, 4, 6, 8, 10, 12, 14, 16];

console.time(‘array length property);

let last element = arry[arry.length – 1];

console.log(lastElement);

console.timeEnd(‘array length property);

console.time(‘array slice method’);

let lastElement1 = arry.slice(-1);

console.log(lastElement1);

console.timeEnd(‘array slice method’);

console.time(‘array pop method’);

let lastElement2 = arry.pop();

console.log(lastElement2);

console.timeEnd(‘array pop method’);

//Output:

//16

//array length property: 13.798ms

//[ 16 ]

//array slice method: 8.839ms

//16

//array pop method: 0.138ms

Conclusion

In this article, we looked at three distinct ways to retrieve the final element in an array using JavaScript:

  • Using the length property
  • Using the slice() method
  • Using the pop() method

Depending on your unique use case and programming style, each method has pros and cons. The most crucial thing is to comprehend how each approach functions and select the one that best meets your requirements. You should now be able to obtain the final element of an array in JavaScript with greater ease. To improve your array manipulation skills, keep using these techniques and practicing.

Read more

Share This Article
Follow:
I'm a tech enthusiast and content writer at TechDyer.com. With a passion for simplifying complex tech concepts, delivers engaging content to readers. Follow for insightful updates on the latest in technology.
Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *