• Skip to primary navigation
  • Skip to main content

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
  • Write for Us
You are here: Home / JavaScript / Using JavaScript to get the Last Element in Array

Using JavaScript to get the Last Element in Array

February 14, 2022 Leave a Comment

In JavaScript, to get the last element in an array we can do this simply by using the length property.

var lastElement = someArray[someArray.length-1]

In the above code, someArray is our array, and to get the last element in it, we have to get the length of the array. We also have to subtract 1 from the length of the array since array indexing starts at 0. So if our array has 6 items, array[5] would get us the last item in the array.


In the example below, we will have an array of numbers. We will use the length property as we did above to get the last number in the array.

var numbersArray = [4,3,5,8,14,67,56,23];
var lastNum = numbersArray[numbersArray.length-1];

The variable lastNum above would have value 23.

Interactive Example of Getting the Last Element of an Array

Below we will provide code to let the user input as many numbers as they want separated by a comma(,).

Here is the simple HTML set up:

<p>Type as many numbers as you want separated by a comma.</p>
<p>If the numbers are not separated by a comma only, this example will not work.</p>
<input id="userArr" type="text" placeholder="1,2,3">
<div id="click-me" onclick="getLastNum()">Get Last Number</div>
<div id="results"></div>

We will then create an array of numbers from this input and find the last value using the length property.

To create an array from the string of numbers the user provides, we will use the split() method along with the Array map() method. We will create a getLastNum function to execute this code.

We will update the results below using the textContent property..

function getLastNum(){
  
  //Get the user input
  var userInput = document.getElementById("userArr").value;

  //Convert user input to an array of numbers
  var numbersArray = userInput.split(',').map(Number);

  //Find the last number
  var lastNum = numbersArray[numbersArray.length-1]

  //Display the max number
  document.getElementById("results").textContent = "The last number in the array is: " + lastNum;

}

The final code and output for this example is below:

Code Output:

Type as many numbers as you want separated by a comma.

If the numbers are not separated by a comma only, this example will not work.

Get Last Number

Full Code:

<p>Type as many numbers as you want separated by a comma.</p>
<p>If the numbers are not separated by a comma only, this example will not work.</p>
<input id="userArr" type="text" placeholder="1,2,3">
<div id="click-me" onclick="getLastNum()">Get Last Number</div>
<div id="results"></div>

<script>

function getLastNum(){
  var userInput = document.getElementById("userArr").value;
  var numbersArray = userInput.split(',').map(Number);
  var lastNum = numbersArray[numbersArray.length-1]
  document.getElementById("results").textContent = "The last number in the array is: " + lastNum;
}

</script>

Hopefully this article has helped you to understand how to use JavaScript to get the last element in an array.

Other Articles You'll Also Like:

  • 1.  Harnessing the Power of forEach in JavaScript: A Comprehensive Guide
  • 2.  Using JavaScript to Round to 2 Decimal Places
  • 3.  JavaScript tanh – Find Hyperbolic Tangent of Number Using Math.tanh()
  • 4.  Using JavaScript to Add Items to Set
  • 5.  Using JavaScript to Return Two Values
  • 6.  JavaScript asinh – Find Hyperbolic Arcsine of Number Using Math.asinh()
  • 7.  Creating a JavaScript Function to Divide Two Numbers
  • 8.  Convert a Set to Array in JavaScript
  • 9.  How to Change an Image on Hover Using JavaScript
  • 10.  Add Minutes to a Date Using JavaScript

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Copyright © 2023 · The Programming Expert · About · Privacy Policy