Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

Array.prototype.toReversed()

Baseline2023
Newly available

ThetoReversed() method ofArray instances is thecopying counterpart of thereverse() method. It returns a new array with the elements in reversed order.

Syntax

js
toReversed()

Parameters

None.

Return value

A new array containing the elements in reversed order.

Description

ThetoReversed() method transposes the elements of the calling array object in reverse order and returns a new array.

When used onsparse arrays, thetoReversed() method iterates empty slots as if they have the valueundefined.

ThetoReversed() method isgeneric. It only expects thethis value to have alength property and integer-keyed properties.

Examples

Reversing the elements in an array

The following example creates an arrayitems, containing three elements, then creates a new array that's the reverse ofitems. Theitems array remains unchanged.

js
const items = [1, 2, 3];console.log(items); // [1, 2, 3]const reversedItems = items.toReversed();console.log(reversedItems); // [3, 2, 1]console.log(items); // [1, 2, 3]

Using toReversed() on sparse arrays

The return value oftoReversed() is never sparse. Empty slots becomeundefined in the returned array.

js
console.log([1, , 3].toReversed()); // [3, undefined, 1]console.log([1, , 3, 4].toReversed()); // [4, 3, undefined, 1]

Calling toReversed() on non-array objects

ThetoReversed() method reads thelength property ofthis. It then visits each property having an integer key betweenlength - 1 and0 in descending order, adding the value of the current property to the end of the array to be returned.

js
const arrayLike = {  length: 3,  unrelated: "foo",  2: 4,};console.log(Array.prototype.toReversed.call(arrayLike));// [4, undefined, undefined]// The '0' and '1' indices are not present so they become undefined

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-array.prototype.toreversed

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp