JS Built-ins
← Array157 / 226
ArrayArray.prototype.reversebeginner

Array.prototype.reverse() β€” non-mutating reverse

πŸ“– Usage Example
const arr = [1, 2, 3, 4, 5]
arr.reverse()  // β†’ [5, 4, 3, 2, 1]

Use reverse() to invert the order of elements in an array in place.

Array.prototype.reverse()

reverse() mutates the original array in place. To avoid mutation, copy the array first with slice() or the spread operator.

const reversed = arr.slice().reverse() // original unchanged
πŸ’‘ Hints (2)
  • β€’ Call `.slice()` first to create a shallow copy, then call `.reverse()` on the copy.
  • β€’ Alternatively, use the spread operator: `[...arr].reverse()`.