Articles by FavTutor
  • AI News
  • Data Structures
  • Web Developement
  • AI Code GeneratorNEW
  • Student Help
  • Main Website
No Result
View All Result
FavTutor
  • AI News
  • Data Structures
  • Web Developement
  • AI Code GeneratorNEW
  • Student Help
  • Main Website
No Result
View All Result
Articles by FavTutor
No Result
View All Result
Home Web Developement

Get Last Element of an Array in JavaScript (with code)

Nikita Arora by Nikita Arora
January 20, 2024
Reading Time: 7 mins read
Get the Last Element of an Array Using JavaScript
Follow us on Google News   Subscribe to our newsletter

JavaScript offers a variety of tools to work with arrays, one of the most basic data types in programming. In this article, we will first learn some simple methods to find the last element in an array in JavaScript.

How to Get the Last Element in a JavaScript Array?

As we know, An array is a collection of similar types of elements i.e. the data type of all elements in an array will be the same. It is an ordered collection, each element has an index.

The index starts from 0 and increases by 1 for every next element. For accessing an element in an array at a particular index, we use square bracket ‘[]’ notation.

Let’s take an example of an array named ‘Ages’. It is a collection of ages of 5 individuals(all numbers). Look at the code below:

// Define an array named 'ages' containing numerical values.
let ages = [20, 23, 25, 27, 30];

// Log the entire 'ages' array to the console.
console.log(ages);

// Retrieve the element at index 2 in the 'ages' array.
let eleAt2 = ages[2];

// Log the retrieved element (element at index 2) to the console.
console.log(eleAt2);

Output:

[ 20, 23, 25, 27, 30 ]
25

Here we declared an array of length 5. Then we accessed the element at index 2. As indices start at 0 ages[2] will correspond to the third element of the array.

Whether it’s for displaying information, performing calculations, or making decisions based on the last item, understanding ways to get the last element is a valuable skill. No matter if you are a beginner or have some experience in coding, we will cover all the methods easily. Let’s get started.

1) Using the Length Property

The simplest approach to get the last element in an array is by using the JavaScript length property. As we know array.length returns the length of the array. We also know that the index starts from 0, so the index of the last element will be length-1. In this method, we can find the last element by accessing the array[array.length-1] element.

In this case, we have obtained the last element of the array without removing it from the array. Here is an example to better understand it:

// Define an array named 'ages' containing numerical values.
let ages = [20, 23, 25, 27, 30];

// Calculate the index of the last element using the array's length property.
let lastEle = ages[ages.length - 1];

// Log the last element to the console.
console.log(lastEle);

Output:

30

Hence we have 30, which is our desired output.

2) Using Slice Method

The slice() method in JavaScript is used to extract a section of the array and it returns the extracted elements in a new array. Therefore in this method, a new array is made.  slice() method takes two parameters in it, which are the starting index and an ending index(optional).

The ending index is not included, only the elements before the ending index are extracted. Moreover, if we don’t pass the ending index then automatically it is assumed to be the last index. 

Now the start index can be negative too i.e. -1 means last element,-2 means second last, and so on. Here is an example:

// Define an array named 'ages' containing numerical values.
let ages = [20, 23, 25, 27, 30];

// Use the slice() method to create a new array containing the last element.
let lastEle = ages.slice(-1);

// Log the new array (containing the last element) to the console.
console.log(lastEle);

Output:

[ 30 ]

As slice() creates a new array the output is in the form of an array. To get in the form of a single element we can access the first(and the only) element of the new array. Here’s  the code :

// Define an array named 'ages' containing numerical values.
let ages = [20, 23, 25, 27, 30];

// Use the slice() method to create a new array containing the last element,
// and then access that element using [0] to get a single value.
let lastEle = ages.slice(-1)[0];

// Log the last element to the console.
console.log(lastEle);

Output:

30

The slice() method is also used to remove the last character in a JavaScript string.

3) Using Pop Method

The pop() method gives us the last element of the array and also removes the last element of the array after returning. This method modifies the original array and the length of the array is shortened by one element after every pop() operation.

Let’s implement it practically:

// Define an array named 'ages' containing numerical values.
let ages = [20, 23, 25, 27, 30];

// Use the pop() method to remove and retrieve the last element from the 'ages' array.
let lastEle = ages.pop();

// Log the retrieved last element to the console.
console.log(lastEle);

// Log the modified 'ages' array (without the last element) to the console.
console.log(ages);

Output:

30
[ 20, 23, 25, 27 ]

As we can see we have got the desired output from the pop() operation. But one more thing to notice is that we have removed the last element from the array. We have now got the modified array when we printed the whole array ages.

4) Using Reverse Method

In this method, we use the reverse() function of JavaScript which is used to reverse the whole array. In this the element that was at the end of the original array is now the first element and the last second element is now the second element and so on. In this method also the original array is modified.

Here is the code:

// Define an array named 'ages' containing numerical values.
let ages = [20, 23, 25, 27, 30];

// Use the reverse() method to reverse the order of elements in the 'ages' array.
ages.reverse();

// Log the first element of the modified 'ages' array (previously the last element) to the console.
console.log(ages[0]);

Output:

30

In this method, we first reversed the array and then we accessed the first element of the array by ages[0]. 

Here is a simple comparison of all the above methods:

Name of MethodOriginal array Modified?New array formed?Rank Acc to speed
Length propertyNoNo4
slice()NoYes3
pop()YesNo1
reverse()YesNo2

According to speed, we have found that the pop() method is the fastest, but considering other factors is also important while choosing the method. As in pop() and reverse() methods the original array is modified so these can be used where modification is allowed and duplicating the array won’t be a good solution.

On the other hand length and slice() methods don’t modify the original array but are comparatively slower than other methods.

Conclusion

We have discussed four different methods to find the last element of the array in JavaScript. Using the right method ensures the effectiveness and efficiency of our code. If you need more assistance, get help for your JavaScript assignment now from top experts.

ShareTweetShareSendSend
Nikita Arora

Nikita Arora

I'm Nikita, a B.Tech Computer Engineering student. My passion lies in web development, data structures, and algorithms. Committed to continuous learning, I find immense joy in sharing insights and fostering collaboration among like-minded peers in web development. Excited about exploring interesting opportunities in technology.

RelatedPosts

Javascript Animation Libraries

Top 10 JavaScript Animation Libraries in 2025

February 19, 2025
JavaScript Interview Questions

Top 40 JavaScript Interview Questions and Answers in 2024

April 1, 2025
Best React UI Component Libraries

10 Must-Know React UI Component Libraries for Web Devs 2024

May 7, 2024
Currying in JavaScript

Currying in JavaScript Explained (with Examples)

March 15, 2024
Javascript Format Currency

Javascript Program for Format Currency

April 8, 2025

About FavTutor

FavTutor is a trusted online tutoring service to connects students with expert tutors to provide guidance on Computer Science subjects like Java, Python, C, C++, SQL, Data Science, Statistics, etc.

Categories

  • AI News, Research & Latest Updates
  • Trending
  • Data Structures
  • Web Developement
  • Data Science

Important Subjects

  • Python Assignment Help
  • C++ Help
  • R Programming Help
  • Java Homework Help
  • Programming Help

Resources

  • About Us
  • Contact Us
  • Editorial Policy
  • Privacy Policy
  • Terms and Conditions

Website listed on Ecomswap. © Copyright 2025 All Rights Reserved.

No Result
View All Result
  • AI News
  • Data Structures
  • Web Developement
  • AI Code Generator
  • Student Help
  • Main Website

Website listed on Ecomswap. © Copyright 2025 All Rights Reserved.