To reverse an array in JavaScript, call array.reverse(), which reverses the elements in place and returns the same array. To keep the original unchanged, use array.toReversed() (ES2023), which returns a new reversed array.
Both methods work on arrays of numbers, strings and objects. Strings have no reverse() method, so you convert them to an array first. The sections below cover each case and the pitfalls.
reverse()toReversed()reverse()Array.prototype.reverse() mutates the array and returns a reference to it.
Because the original changes, do not assign the result expecting a copy. The returned value is the same array, not a new one.
toReversed()toReversed() is part of ES2023. It returns a reversed copy and leaves the source untouched. It is supported in current versions of Chrome, Edge, Firefox, Safari and in Node.js 20 and later.
This is the best choice for state in frameworks such as React, where you should not mutate arrays that are part of state.
When toReversed() is unavailable, copy first, then reverse the copy.
The spread operator and slice() both create a shallow copy, so the original order is preserved.
reverse() only changes the order of elements. It does not touch the contents of each element, so it works the same for every type.
A shallow copy shares the object references. Changing people[0].age after copying changes it in every copy too.
Strings are immutable and have no reverse() method. Turn the string into an array of characters, reverse it and join it.
Spreading with [...text] splits by Unicode code point, which handles emoji and other characters outside the Basic Multilingual Plane. The older text.split("") splits by UTF-16 code unit and breaks those characters. Combining sequences such as accented letters built from several code points can still be reordered incorrectly, so use Intl.Segmenter when you must reverse by user-visible characters.
Typed arrays such as Uint8Array have their own reverse() and toReversed(). To iterate backwards without reversing, use a plain loop or toReversed():
Array.prototype.findLast() and at(-1) also cover many cases where you only wanted the last items.
Interview questions often ask for an in-place reversal without reverse(). Swap elements from both ends until the pointers meet.
This runs in linear time and constant extra memory, the same as the built-in method. In production code prefer reverse(), which is simpler and well optimised.
reverse() return a new array?No. It reverses the array in place and returns the same array reference.
Use toReversed() in modern environments, or [...array].reverse() elsewhere.
reverse()?Swap elements from both ends with a loop: for (let i = 0, j = a.length - 1; i < j; i++, j--) [a[i], a[j]] = [a[j], a[i]];.
reverse() work on sparse arrays?Yes, but holes move with their positions. Avoid sparse arrays when order matters.
Use reverse() when you want to mutate the array and toReversed() (ES2023) when you want a copy. Fall back to [...array].reverse() for older runtimes, and reverse strings by spreading them into an array, reversing and joining.