All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Get Last Element Of Array In JavaScript

Last Updated : Mar 11, 2024

How To Get Last Element Of Array In JavaScript

In this article we will show you the solution of how to get last element of array in JavaScript, to get the last element of the array in javascript we are going to use two different methods. Let us understand the functions first

  • Array.length: this property in javascript returns the number of elements in the array. now to get the last element we have to set the array length to the last position and set it to another array
  • At() method: this property returns the last element if the at() set with the negative index.

Step By Step Guide On How To Get Last Element Of Array In JavaScript :-

Method 1 - array.length method

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title> how to get last element of array in javascript </title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css">
    <style>
        h1 {
          color : rgb(95, 194, 95) ;
          font-size: 25px;
          font-weight : bolder ;
        }
    </style>
</head>
<body>
    <center>
        <h1> TALKERSCODE </h1>
        <h3> how to get last element of array in javascript </h3>
    </center>
    <script>
     let arr = ['a', 'b', 'c', 'd', 'e', 'f'] ;
            let last = arr[arr.length-1] ;
            console.log(last) ;
    </script>
</body>
</html>
  1. To write HTML code <! DOCTYPE html> to mention which version of the HTML file is written in.
  2. Then we have to write the <html> tag used to define the root of an HTML document. And also write the ending tag </html>.
  3. <head> tag used here to use to contain the metadata about the HTML file and end it with </head> tag
  4. Then <title> tag is used to set the title of the HTML document and end it with </title> tag.
  5. we will use an external CSS file to style the html page
  6. using <style> tag to add CSS
  7. <h1> tag used to add a heading close it with </h1> tag
  8. Create a <Script> tag to write JavaScript within it.
  9. Creating an array with some values using let
  10. Another array is created to store the array last element using array.length-1
  11. Use console.log() to display the last element

Method 2 - at() method

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title> how to get last element of array in javascript </title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css">
    <style>
        h1 {
          color : rgb(95, 194, 95) ;
          font-size: 25px;
          font-weight : bolder ;
        }
    </style>
</head>
<body>
    <center>
        <h1> TALKERSCODE </h1>
        <h3> how to get last element of array in javascript </h3>
    </center>
    <script>
        const arr = [10, 20, 30, 40, 50, 60, 70, 80] ;
        let lastItem = arr.at(-1) ;
        console.log(lastItem) ;
    </script>
</body>
</html>
  1. To write HTML code <! DOCTYPE html> to mention which version of the HTML file is written in.
  2. Then we have to write the <html> tag used to define the root of an HTML document. And also write the ending tag </html>.
  3. <head> tag used here to use to contain the metadata about the HTML file and end it with </head> tag
  4. Then <title> tag is used to set the title of the HTML document and end it with </title> tag.
  5. we will use an external CSS file to style the html page
  6. using <style> tag to add CSS
  7. <h1> tag used to add a heading close it with </h1> tag
  8. Create a <Script> tag to write JavaScript within it.
  9. Creating an array with some values using const
  10. Using at() with negative index that returns the last element
  11. Use console.log() to display the last element

Conclusion :-

At last, here in conclusion, we can say that with this article’s help, we know how to get the last element of array in javascript.

I hope this article on how to get last element of array in JavaScript helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪