Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Array.prototype.toReversed: a reversed copy

toReversed creates a reversed shallow copy without changing the receiver and materializes holes as undefined.

Version: ECMAScript 2023; example target: Node.js 24.22.2

Syntax

array.toReversed()

array.toReversed() reads the receiver length, creates a new ordinary Array, and writes values in reverse order. It takes no arguments, does not change array, and does not consult Symbol.species; element objects retain the same references because the copy is shallow.

Unlike reverse(), the result is always dense: reading an empty slot yields undefined, then an own property with that value is created in the copy. The method is generic and works on array-like values through length and indexed properties.

A null or undefined receiver throws TypeError. Failures from length conversion, getters, and proxy traps propagate; an array-like length too large for an ordinary Array can produce RangeError. The method was standardized in ECMAScript 2023.

Examples

const source = [{ id: "a" }, { id: "b" }, { id: "c" }];
const copy = source.toReversed();
console.log(copy.map(item => item.id).join(","));
console.log(source.map(item => item.id).join(","));
console.log(copy !== source, copy[0] === source[2]);

const sparse = [1, , 3];
const dense = sparse.toReversed();
console.log(dense.length, 0 in dense, 1 in dense, 2 in dense, dense[1] === undefined);

Result

c,b,a
a,b,c
true true
3 true true true true

Sources