Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JavaScript · Function

reverse

Array.prototype.reverse() reverses an array in place and returns the same array reference. Use toReversed() or copy the array first when the original must remain unchanged.

How it works

Calling reverse() swaps elements from the ends toward the center, changes the original array, and returns that same array reference. It works with array-like objects that provide a length and integer-keyed properties, and it preserves sparse-array empty slots.

Syntax

reverse()

Returns

A reference to the original array, with its elements reversed.

Key points

  • It reverses element order in place and returns the same array object.
  • The first and last elements exchange positions, continuing inward.
  • The return value is the original array after mutation.
  • Empty slots are preserved when an array is reversed.
  • The method can operate on array-like objects with a length and integer-keyed properties.

Common mistakes

  • reverse() mutates the original array rather than creating a new one.
  • The returned value aliases the original array; changes through either reference affect the same object.
  • Sparse arrays stay sparse, so reversing does not turn empty slots into explicit undefined values.

Example: Reverse an array in place

const items = [1, 2, 3];
const result = items.reverse();

console.log(items);  // [3, 2, 1]
console.log(result === items); // true

Reverses the array in place and confirms that the returned value is the same array object.

When to use it

Reverse an array in place

Mutate an existing array when reverse order is needed.

Preserve the original

Create a reversed result without changing source data.

Handle array-like values

Reverse array-like objects that expose length and integer-keyed properties.

  • Array.prototype.toReversed()
  • Array.from()
  • Spread syntax (...)

Source: Array.prototype.reverse() - JavaScript - MDN Web Docs. Checked 2026-10-15.