arr.toReversed()
returns a copy of arr reversed
it is a function on the array prototype
reversing an array, creates a copy
WHEN calling toReversed()
THEN it returns the content in reversed order
WHEN calling toReversed()
THEN the original array stays the same
WHEN calling toReversed()
THEN the original and the result are not the same array
in contrast WHEN calling reverse()
βΌοΈ THEN the original and the reversed array are the same
using toReversed()
with non-arrays
WHEN used on a string THEN it reverses the characters AND returns an array of characters
WHEN used on an array-like object THEN it uses the length
property to identify the number of items
WHEN used on an object with numeric keys in it THEN it also uses those as values GIVEN they are within length
in-depth
WHEN using toReversed()
on a sparse array THEN it uses the empty items as if they has the value undefined
WHEN using a numeric string as index THEN this is equal a number as index (which is not unique to toReversed()
)
WHEN reversing an array with objects in them THEN these objects are NOT copied, but stay references
WHEN the used objects have no "outside" reference THEN they are still the same ones after the reversal of the array that contain them
Links
The repository where the proposal was worked on. Make sure to read the polyfill.js source code in there, shows very well how `toReversed()` works.
The "ECMAScript Language Specification", the JavaScript specification text describing this function how an engine must implement it.
The Mozilla Developer Network docs.
Related Katas
Array API
array.sort()
basicsarray.sort()
can take a compare functionarray.shift()
(as introduced in ES3)array.push()
(as introduced in ES3)Array.from()
Array.of()
array.fill()
array.find()
array.findIndex()
array.entries()
array.keys()
array.values()
array.includes()
array.toReversed()
Difficulty Level
BEGINNER
First Published
16 January 2024
Stats
12 tests to solve