How to Get the Last Index of an Array using JavaScript

How to Get the Last Index of an Array using JavaScript


In this tutorial we will create a How to Get the Last Index of an Array using JavaScript. This tutorial purpose is teach you how to get the last data from an array. This will cover all the basic function will change your background I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to any application if you want to apply an array object to you system. I will give my best to provide you the easiest way of creating this program Get Last index of an array. So let's do the coding.

Before we get started:

This is the link for the template that i used for the layout design

Creating The Interface

This is where we will create a simple interface to our application. This code will only table with data list and button. To create this simply copy and write it into your text editor, then save it as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <nav class="navbar navbar-default">
  8. <div class="container-fluid">
  9. <a class="navbar-brand" href="">Sourcecodester</a>
  10. </div>
  11. </nav>
  12. <div class="col-md-3"></div>
  13. <div class="col-md-6 well">
  14. <h3 class="text-primary">How to Get the Last Index of an Array using JavaScript</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-4">
  17. <table class="table table-bordered">
  18. <tr class="alert-info">
  19. <th>My Array List</th>
  20. </tr>
  21. </thead>
  22. <tbody class="alert-success" id="result">
  23. </tbody>
  24. </table>
  25. </div>
  26. <div class="col-md-8">
  27. <button class="btn btn-primary" id="get">Get Last Index</button>
  28. <br /><br />
  29. <div id="display"></div>
  30. </div>
  31. </div>
  32. <script src="script.js"></script>
  33. </body>
  34. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will get the last data from an array when the button is clicked. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. let myArray = ["John Smith", "Jake Cyrus", "Steve Roger", "Tony Stark", "Bruce Banner", "Clark Thompson"];
  3. displayData(myArray);
  5. function displayData(myArray){
  6. let html="";
  7. for(var i=0; i<myArray.length; i++){
  8. html +="<tr>";
  9. html +="<td>"+myArray[i]+"</td>";
  10. html +="</tr>";
  11. }
  13. document.getElementById('result').innerHTML=html;
  14. }
  17. function getLastIndex(myArray){
  18. let data = myArray[myArray.length - 1];
  19. document.getElementById('display').innerHTML="<center><label>The last index of an Array is: <span class='text-primary'>"+data+"</span></label></center>";
  20. }
  22. document.getElementById("get").addEventListener("click", getLastIndex.bind(this, myArray));

In the code above we first create an array object called myArray(). Next we will create a method that will display the array data into html table called displayData(). And last we will created a method called getLastIndex(), this function will try to get only the last data from an array object. The trick here is that we will check the position of the last data of an array by subtracting the length of the array.


The How to Get the Last Index of an Array using JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Get the Last Index of an Array using JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for JavaScript Language

JavaScript Tutorials

Add new comment