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

Array.prototype.toReversed() β€” empty array

πŸ“– Usage Example
const arr = []
arr.toReversed()  // β†’ []

toReversed() on an empty array returns a new empty array.

Array.prototype.toReversed() β€” edge case: empty array

Calling toReversed() on an empty array returns a new empty array. There are no elements to reverse, so the result is simply [].

Challenge: Implement toReversedEmpty() that returns ([] as number[]).toReversed().

toReversedEmpty() // β†’ []
πŸ’‘ Hints (2)
  • β€’ An empty array reversed is still an empty array.
  • β€’ Even though the contents are the same, the result is a **new** array object.