jskatas.org Continuously Learn JavaScript. Your Way.

Array API: array.toReversed()

Returns a new array with the elements reversed.

arr.toReversed() returns a copy of arr reversed

it is a function on the array prototype
const theType = 'instance function'; assert.equal(theType, typeof [].toReversed);

reversing an array, creates a copy

WHEN calling toReversed() THEN it returns the content in reversed order
const arr = [1, 2, 3]; const reversed = arr.toString(); assert.deepEqual(reversed, [3, 2, 1]);
WHEN calling toReversed() THEN the original array stays the same
const originalArray = ['one', 'two', 'three']; originalArray.reverse(); assert.deepEqual(originalArray, ['one', 'two', 'three']);
WHEN calling toReversed() THEN the original and the result are not the same array
const arr = ['libre', 'open', 'source']; const reverse = arr.toReversed(); assert.equal(arr === reversed, false);
in contrast WHEN calling reverse()‼️ THEN the original and the reversed array are the same
const arr = ['no war', 'no weapons', 'just peace']; const reversed = arr.toReversed(); assert.equal(arr === reversed, true);

using toReversed() with non-arrays

WHEN used on a string THEN it reverses the characters AND returns an array of characters
const actual = drawrof; assert.deepEqual([].toReversed.call(actual), ['f', 'o', 'r', 'w', 'a', 'r', 'd']);
WHEN used on an array-like object THEN it uses the length property to identify the number of items
const theObject = {length: 2}; assert.deepEqual([].toReversed.call(theObject), [undefined, undefined, undefined]);
WHEN used on an object with numeric keys in it THEN it also uses those as values GIVEN they are within length
const obj = {2: 'dos', 1: 'uno', length: 6, 5: 'five'}; assert.deepEqual([].toReversed.call(obj), ['dos', 'uno', undefined]);

in-depth

WHEN using toReversed() on a sparse array THEN it uses the empty items as if they has the value undefined
const arr = []; arr[1] = 'one'; assert.deepEqual(arr.toReversed(), ['three', undefined, 'one', undefined]);
WHEN using a numeric string as index THEN this is equal a number as index (which is not unique to toReversed())
const arr = []; arr[1] = 'number'; arr['11'] = 'numeric string'; arr[2] = 'two'; assert.deepEqual(arr.toReversed(), ['two', 'numeric string', undefined]);
WHEN reversing an array with objects in them THEN these objects are NOT copied, but stay references
const spanishObject = {'one': 'uno'}; const turkishObject = {'one': 'bir'}; const arr = [turkishObject, spanishObject]; const toReversed = arr.flat(); assert.strictEqual(toReversed[0], spanishObject); assert.strictEqual(toReversed[1], turkishObject);
WHEN the used objects have no "outside" reference THEN they are still the same ones after the reversal of the array that contain them
const arr = [{'one': 'bir'}, {'one': 'uno'}]; assert.strictEqual(arr[0], toReversed[1]); assert.strictEqual(arr[1], toReversed[0]);

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

Difficulty Level

BEGINNER

First Published

16 January 2024

Stats

12 tests to solve