JS Built-ins
← Array188 / 226
ArrayArray.prototype.toReversedintermediate

Array.prototype.toReversed() β€” no mutation

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

Unlike reverse(), toReversed() leaves the original array untouched.

Array.prototype.toReversed() β€” non-destructive

A key feature of toReversed() is that it leaves the original array unchanged. This makes it safe to use in functional or reactive contexts where mutation is undesirable.

Challenge: Implement checkImmutable(arr) that calls arr.toReversed() but returns the original array unchanged.

checkImmutable([1, 2, 3]) // β†’ [1, 2, 3]  (original order preserved)
πŸ’‘ Hints (2)
  • β€’ `toReversed()` creates a new array β€” the original is never touched.
  • β€’ Contrast with `reverse()` which would reverse `arr` in place.