Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Use arr.reverse() to reverse an array in place. To get the reversed order without changing the original, use arr.toReversed() when your runtime supports it, or reverse a shallow copy with [...arr].reverse().

Reverse an array in place with reverse()

reverse() changes the array it is called on and returns that same array reference:

const values = [1, 2, 3];

const result = values.reverse();
console.log(values); // [3, 2, 1]
console.log(result === values); // true

Because the original array changes, every variable or object property that refers to it sees the reversed order. Assigning the return value to a different variable does not preserve the previous order.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Keep the original order unchanged

Use toReversed() when supported

toReversed() creates and returns a new array, leaving the original array’s order unchanged:

const values = [1, 2, 3];
const reversed = values.toReversed();

console.log(reversed); // [3, 2, 1]
console.log(values);   // [1, 2, 3]

TypeScript includes the method’s declaration in its ES2023 library APIs, as documented in the TSConfig lib reference. However, TypeScript declarations only tell the compiler which APIs to recognize; they do not add the method to the JavaScript runtime. Check that the runtime where your code executes supports toReversed(), or use a suitable polyfill or the copy-first alternative below. Since a project’s target and lib settings can differ, a successful type check alone does not prove runtime support.

Use a copy-first fallback

For runtimes without toReversed(), copy the array and then reverse the copy:

Rank #2
TypeScript Programming Language - Software Engineer & Coder T-Shirt
  • TypeScript implements a superset of syntax for strictly typed development, facilitating deep static analysis and enhanced development environment integration. The compiler translates source into standard script formats, ensuring parity across any runtime.
  • TypeScript is ideal for front-end developers, full-stack engineers, and software architects who build large-scale web applications. It serves those looking to improve code excellence, reduce bugs through static checking, and maintain complex projects more.
  • Lightweight, Classic fit, Double-needle sleeve and bottom hem
const values = [1, 2, 3];
const reversed = [...values].reverse();
// Equivalent: values.slice().reverse()

Both expressions leave the original array’s order unchanged. They create a shallow copy, not a deep copy: if an element is an object, the original and copied arrays still refer to that same object. See MDN’s explanation of Array reference and copying.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose the method that matches your intent

  • To change the existing mutable array, call arr.reverse().
  • To return a new reversed array on a runtime that supports it, call arr.toReversed().
  • To return a new reversed array without relying on toReversed(), use [...arr].reverse() or arr.slice().reverse().

A variable typed as readonly T[] cannot be mutated through that readonly type. TypeScript describes ReadonlyArray as a type for arrays that should not be changed in its Object Types handbook. Use a copy-first expression for a mutable reversed result, or toReversed() when both the library declarations and runtime support it.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

How sparse arrays behave

For ordinary dense arrays, the methods produce the expected reversed element order. Sparse arrays have a distinction: reverse() preserves empty slots in their corresponding reversed positions, while toReversed() reads holes as undefined and returns a dense array at those positions. Consult MDN’s references for reverse() and toReversed() if your code relies on sparse-array behavior.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.