JavaScript Glossary: Array .reverse() Method

Publikováno: 8.2.2019

Basics

This method changes the position of elements within the array. The first element goes to the last position and the last element goes to the first position. The method returns the re...

Celý článek

Basics

This method changes the position of elements within the array. The first element goes to the last position and the last element goes to the first position. The method returns the reversed array.

[1, 2, 3, 4, 5].reverse()
// [5, 4, 3, 2, 1]

https://scotch.io/embed/gist/a2483d2d7330b14016a4327cdf5c3019

Syntax

array.reverse()

No Parameters

Returns the reversed array

The method will return the reversed array.

const numbers = [1, 2, 4, 5, 6, 7];
const reversed = numbers.reverse();
// reversed: Array [7, 6, 5, 4, 2, 1]

Take care when using .reverse() as it mutates the calling array

Common Uses and Snippets

Return order of items in array

When an array of elements are returned in a reversed order. The reverse method will restore order.

https://scotch.io/embed/gist/cef2698e4078c9d3d52ba540574233f8

Nahoru
Tento web používá k poskytování služeb a analýze návštěvnosti soubory cookie. Používáním tohoto webu s tímto souhlasíte. Další informace