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

Actual Wizard

Great Content From Actual Wizards

  • Blogging
  • Programming
  • Social Media
  • Marketing
You are here: Home / Programming / JavaScript

How to Get the Last Element of an Array in JavaScript

November 17, 2020 by Kevin Marszalek

There are a number of different ways to get the last element of an array in JavaScript.

Below we will explore those techniques, each with a simple example.

The Most Common Technique Using length() -1

The simplest way to get the last element of an array in JavaScript is to subtract one from the length of the array and then use that value as the index to assign the last item to a variable.

This sounds complicated, but it’s quite easy to do.

Example:

var demoArray = ["Apple", "Ebay", Google", "Microsoft"];

var lastItem1 = demoArray[demoArray.length() - 1];

An Alternative Technique Using slice()

The second way is to use the array.slice() method to create a temporary array that contains the range of values from index negative one to index zero. To be clear, the slice starts at the last value in the array and ends at the first value of the array. This method is frowned upon as it can be hard to understand the code.

Example:

var lastItem2 = demoArray.slice(-1)[0];

pop() Can Also Be Used To Get the Last Item

The third way involves using the array.pop() method as it will return the value of the last element in the array. The side effect of this technique is that the last element of the array will be removed. Depending on the situation, that may or may not be desirable.

Example:

var lastItem3 = demoArray.pop();

Final Solution: Just Make a Function

The final way is to create a function to handle this all for you. This is very useful if you are frequently creating code that tries to access that last element of an array.

Example:

function last(array) {
    return array[array.length - 1];
}

var lastItem4 = last(demoArray);

Filed Under: JavaScript

Read More From Actual Wizard

  • Understanding the Different Parts of an Email Address
    An email address has four parts; the recipient name, the @ symbol, the domain name, and the top-level domain. …
  • How to use document.write() in JavaScript
    How to use document.write() in JavaScript The document.write() function is commonly used when testing simple …
  • How to Open a New Tab with JavaScript
    Opening a new browser window in a new tab can be done easily in JavaScript. Modern web browsers provide a …
  • The Difference Between a Franchisor and a Franchisee Explained
    The franchised business system has two sides that play different roles in the business. The franchisor …

Primary Sidebar

More posts from this section

  • How to Get the Last Element of an Array in JavaScript
  • How to use document.write() in JavaScript
  • How to Open a New Tab with JavaScript
  • How to Convert an HTML Node to a String in JavaScript
  • JavaScript String Anchor Method Explained
  • JavaScript String Big Method Explained
  • JavaScript String Blink Method Explained
  • JavaScript String Bold Method Explained
  • JavaScript String charAt Method Explained
  • JavaScript String charCodeAt Method Explained

Copyright © 2025 ActualWizard.com

  • About
  • Terms of Service
  • Privacy Policy
  • Cookie Policy