Skip to content

WebDevHubs

  • Home
  • JavaScript
  • Toggle search form

Get the Last Item of an Array in JavaScript

Posted on December 18, 2024 By Admin No Comments on Get the Last Item of an Array in JavaScript

Given an array, the task is to get the last item of an array in JavaScript. There are several ways to get the last item of an array.

1. Using Bracket Notation

Arrays in JavaScript are zero-indexed, meaning the first item is at index 0. You can use the length of the array to calculate the index of the last element.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let arr = [10, 20, 30, 40, 50];
let lastItem = arr[arr.length - 1];
console.log(lastItem); // Output: 50
let arr = [10, 20, 30, 40, 50]; let lastItem = arr[arr.length - 1]; console.log(lastItem); // Output: 50
let arr = [10, 20, 30, 40, 50];
let lastItem = arr[arr.length - 1];
console.log(lastItem); // Output: 50

2. Using slice() Method

The slice() method can extract a portion of an array. By providing -1 as the argument, it retrieves the last element as a new array.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let arr = [10, 20, 30, 40, 50];
let lastItem = arr.slice(-1)[0];
console.log(lastItem); // Output: 50
let arr = [10, 20, 30, 40, 50]; let lastItem = arr.slice(-1)[0]; console.log(lastItem); // Output: 50
let arr = [10, 20, 30, 40, 50];
let lastItem = arr.slice(-1)[0];
console.log(lastItem); // Output: 50

3. Using at() Method (ES2022)

The at() method allows you to access elements using positive or negative indices. To get the last item, you can use -1 as the index.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let arr = [10, 20, 30, 40, 50];
let lastItem = arr.at(-1);
console.log(lastItem); // Output: 50
let arr = [10, 20, 30, 40, 50]; let lastItem = arr.at(-1); console.log(lastItem); // Output: 50
let arr = [10, 20, 30, 40, 50];
let lastItem = arr.at(-1);
console.log(lastItem); // Output: 50

4. Using pop() Method

The pop() method removes and returns the last item of an array. This is destructive and changes the original array.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let arr = [10, 20, 30, 40, 50];
let lastItem = arr.pop();
console.log(lastItem); // Output: 50
console.log(arr); // Output: [10, 20, 30, 40]
let arr = [10, 20, 30, 40, 50]; let lastItem = arr.pop(); console.log(lastItem); // Output: 50 console.log(arr); // Output: [10, 20, 30, 40]
let arr = [10, 20, 30, 40, 50];
let lastItem = arr.pop();
console.log(lastItem); // Output: 50
console.log(arr);  // Output: [10, 20, 30, 40]

5. Using a for Loop

For scenarios where you want to retrieve the last item manually, a for loop can be used. While not the most efficient, it demonstrates how to iterate through an array.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let arr = [10, 20, 30, 40, 50];
let lastItem;
for (let i = 0; i < arr.length; i++) {
if (i === arr.length - 1) {
lastItem = arr[i];
}
}
console.log(lastItem); // Output: 50
let arr = [10, 20, 30, 40, 50]; let lastItem; for (let i = 0; i < arr.length; i++) { if (i === arr.length - 1) { lastItem = arr[i]; } } console.log(lastItem); // Output: 50
let arr = [10, 20, 30, 40, 50];
let lastItem;

for (let i = 0; i < arr.length; i++) {
    if (i === arr.length - 1) {
        lastItem = arr[i];
    }
}

console.log(lastItem); // Output: 50

6. Using reduce() Method

The reduce() method can be used to iterate over the array and return the last item.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let arr = [10, 20, 30, 40, 50];
let lastItem = arr.reduce((_, current) => current);
console.log(lastItem); // Output: 50
let arr = [10, 20, 30, 40, 50]; let lastItem = arr.reduce((_, current) => current); console.log(lastItem); // Output: 50
let arr = [10, 20, 30, 40, 50];
let lastItem = arr.reduce((_, current) => current);
console.log(lastItem); // Output: 50

7. Using length and Destructuring (ES6)

Destructuring assignment combined with the length property can be used to get the last item in a concise manner.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let arr = [10, 20, 30, 40, 50];
let [lastItem] = [arr[arr.length - 1]];
console.log(lastItem); // Output: 50
let arr = [10, 20, 30, 40, 50]; let [lastItem] = [arr[arr.length - 1]]; console.log(lastItem); // Output: 50
let arr = [10, 20, 30, 40, 50];
let [lastItem] = [arr[arr.length - 1]];
console.log(lastItem); // Output: 50
JavaScript, Web Technologies Tags:JavaScript-Array, JavaScript-Questions

Post navigation

Previous Post: How Do I Check if a Variable Is an Array in JavaScript?
Next Post: Copy Array by Value in JavaScript

More Related Articles

How Can I Remove a Specific Item from an Array in JavaScript? JavaScript
JavaScript Array toLocaleString() Method JavaScript
JavaScript Array push() Method JavaScript
JavaScript Array reverse() Method JavaScript
How to Convert Set to an Array in JavaScript? JavaScript
JavaScript Array some() Method JavaScript

Leave a Reply Cancel reply

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

Archives

  • May 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024

Categories

  • CSS
  • HTML
  • JavaScript
  • Lodash
  • PHP
  • Python
  • Uncategorized
  • Web Technologies
  • Web Templates

Recent Posts

  • How to Center Text in HTML?
  • Design a Simple HTML Page | First HTML Project
  • Best Way to Initialize an Empty Array in PHP
  • HTML Description Lists
  • HTML Ordered Lists

Recent Comments

No comments to show.

Copyright © 2025 WebDevHubs.

Powered by PressBook Green WordPress theme