JS Built-ins
← Array187 / 226
ArrayArray.prototype.toReversedbeginner

Array.prototype.toReversed() β€” immutable reverse

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

Use toReversed() to get a reversed copy of an array without mutating the original.

Array.prototype.toReversed()

Array.prototype.toReversed() returns a new array with the elements in reversed order. Unlike reverse(), it does not mutate the original array.

Challenge: Implement reverseImmutable(arr) that returns arr.toReversed().

reverseImmutable([1, 2, 3]) // β†’ [3, 2, 1]
reverseImmutable([10, 20])  // β†’ [20, 10]
πŸ’‘ Hints (2)
  • β€’ `toReversed()` is the immutable counterpart of `reverse()`.
  • β€’ Available in modern JavaScript (ES2023). Check compatibility if targeting older environments.