To remove the last element from a JavaScript array, call Array.prototype.pop() when mutating the array in place is acceptable, or use a non-mutating approach like Array.prototype.slice(0, -1) or Array.prototype.toSpliced(-1, 1) when you need to preserve the original array reference. Using pop() removes the final item in O(1) time and returns it (or undefined if the array is empty), while returning a new array avoids unintended side effects in shared or immutable state.


1. Removing the Last Element in Place with pop()

When you want to shorten the existing array directly and optionally retrieve the removed item, arr.pop() is the most direct built-in method:

// Remove and return the last element in place
function popLastElement(items) {
  if (!Array.isArray(items)) {
    throw new TypeError("Expected items to be an array.");
  }

  return items.pop(); // Returns undefined if empty
}

// Example usage:
const queue = ["task-1", "task-2", "task-3"];
const removed = popLastElement(queue);

console.log(removed); // "task-3"
console.log(queue);   // ["task-1", "task-2"]
  • Mutates original array: pop() decreases arr.length by 1 and returns the removed value (or undefined on an empty array).
  • Watch for shared references: Mutating an array in place can affect other variables pointing to the same array.

2. Returning a New Array Without Mutating the Original

When you must keep the source array unchanged—such as in state updates or pure helper functions—use slice(0, -1) or ES2023 toSpliced(-1, 1):

// Return a new array without the last element, leaving the original intact
function withoutLastElement(items) {
  if (!Array.isArray(items)) {
    throw new TypeError("Expected items to be an array.");
  }

  return items.slice(0, -1);
}

// Example usage:
const scores = [10, 20, 30, 40];
const trimmedScores = withoutLastElement(scores);
const splicedScores = scores.toSpliced(-1, 1);

console.log(trimmedScores); // [10, 20, 30]
console.log(splicedScores); // [10, 20, 30]
console.log(scores);        // [10, 20, 30, 40]
  • Negative index support: Both slice(0, -1) and toSpliced(-1, 1) count backward from the end and return [] on an empty array.
  • Shallow copy behavior: Neither method deeply clones nested objects or arrays inside the remaining elements.

3. Truncating via length vs. Using pop()

Adjusting arr.length = arr.length - 1 also trims the last item in place when the array is non-empty, but calling arr.length = 0 - 1 on an empty array throws a RangeError unless guarded:

// Safe in-place truncation compared with pop()
const letters = ["a", "b", "c"];
if (letters.length > 0) {
  letters.length -= 1;
}

console.log(letters); // ["a", "b"]
  • Prefer pop() for clarity: pop() communicates intent immediately and safely returns undefined on an empty array.
  • Performance trade-off: pop() runs in O(1) time, whereas slice(0, -1) and toSpliced(-1, 1) allocate and copy n - 1 elements in O(n) time.

TL;DR: Use arr.pop() to remove and return the last element in place (O(1), returning undefined if empty), or use arr.slice(0, -1) / arr.toSpliced(-1, 1) to return a new array without mutating the original.