Search Authority

How to Get the Last Item in an Array in JavaScript: Quick Guide

When working with JavaScript arrays, developers often need to retrieve the last element quickly. This task can be done in multiple ways, and choosing the right method affects re...

Mara Ellison
How to Get the Last Item in an Array in JavaScript: Quick Guide

When working with JavaScript arrays, developers often need to retrieve the last element quickly. This task can be done in multiple ways, and choosing the right method affects readability and performance.

Understanding how to get last item in array javascript helps you avoid common bugs and write safer utility code. The following sections break down the most reliable patterns and best practices.

Method Syntax Returns Empty Array Mutates Original
array[array.length - 1] const last = arr[arr.length - 1] undefined No
array.slice(-1) const last = arr.slice(-1) [] No
array.at(-1) const last = arr.at(-1) undefined No
pop on copy const last = [...arr].pop() undefined No (original unchanged)

Using length minus one indexing

The most traditional way to get last item in array javascript is by using array length minus one. This approach works in all JavaScript environments and is easy to understand.

Keep in mind that accessing an index on an empty array returns undefined, so you should check the length before reading. This pattern has zero function call overhead and is very fast.

Using at with negative index

How at works

The at method provides a modern way to get last item in array javascript using negative indices. It accepts -1 for the last element, -2 for the second last, and so on.

At works on sparse arrays and returns undefined for out-of-range indices, making it a concise and safe choice for many developers.

Using slice to copy the last element

Slice behavior with negative start

You can call slice(-1) to get last item in array javascript while returning a new shallow-copied array. This is useful when you want to preserve immutability and avoid direct index access.

Because slice always returns an array, you may need to unwrap it if you expect a single element or need to chain methods that expect scalar values.

Robust patterns and edge cases

Handling edge cases like sparse arrays, non-array objects, or undefined/null values makes your utility more reliable. Wrapping the logic in a small function ensures consistent behavior across your codebase.

Performance differences are usually negligible, but choosing the right pattern can improve readability and reduce subtle bugs in larger applications. Favor clarity unless profiling shows a bottleneck.

Best practices and recommendations

  • Check array length before using index-based access if undefined has special meaning in your logic.
  • Use at(-1) for clean syntax when targeting modern environments that support ES2022.
  • Choose slice(-1) when you need an array result or want to keep your code immutable.
  • Avoid mutating the original array with pop unless removal is part of your intended workflow.
  • Wrap the logic in a small utility function to standardize behavior across your project.

FAQ

Reader questions

What happens if the array is empty when using at(-1) or length-based access?

Both approaches return undefined, so you should guard against empty arrays when a missing value has different semantic meaning in your app.

Does slice(-1) change the original array?

No, slice returns a new array and leaves the original input untouched, which is helpful when working with immutable patterns.

Can I use pop directly to get the last item safely?

Using pop directly mutates the original array, so prefer read-only methods unless you intentionally want to remove the last element.

How do I safely get the last item from any list-like object such as arguments or NodeList?

Convert the list-like object to a real array with Array.from or the spread operator, then use at(-1) or another method to safely retrieve the last entry.

Related Reading

More pages in this topic cluster.

Who Designed the Nike Logo? The Story Behind the Swoosh

The Nike swoosh is one of the most recognizable symbols in the world, but few people know the story behind its creation. This piece explores who designed the Nike logo, why it h...

Read next
What is the World's Hottest Pepper? 🌶️🔥

When people ask about the world's hottest pepper, they usually mean the variety that currently holds the Guinness World Record and pushes the boundaries of capsaicin heat. Peppe...

Read next
Jon Huertas in This Is Us:角色, 出演时期与剧情影响详解

Jon Huertas 在《这就是我们》中饰演成年 Kevin Pearson,这一角色从2016年首播持续至2022年最终季,构成了剧集核心家庭叙事的重要组成部�...

Read next