• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
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.  Using JavaScript to Resize an Image
  • 2.  Math floor JavaScript – Using Math.floor() to Round Down to Floor
  • 3.  JavaScript Exponent – Exponentiate Numbers with Math.pow()
  • 4.  Using JavaScript to Run a Function Every 5 seconds
  • 5.  How to Remove Non Numbers From String in JavaScript
  • 6.  Insert Character Into String In JavaScript
  • 7.  JavaScript Sorted Map – How to Sort a Map by Keys or Values
  • 8.  JavaScript Square Root with Math.sqrt() Method
  • 9.  Using JavaScript to Get the Base URL
  • 10.  How to Repeat Character N Times in 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 *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

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