October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

How to Reverse an Array in TypeScript

Use reverse() to mutate an array, or toReversed() or a shallow copy when you need to preserve its original order.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use arr.reverse() to reverse an array in place. To get the reversed order without changing the original array, use arr.toReversed() where the 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:

As an Amazon Associate I earn from qualifying purchases.

const values = [1, 2, 3];

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

Because it mutates the original, any other variable referring to values sees the reversed order too. Assigning the return value to a new variable does not preserve the old array.

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

Keep the original order

Use toReversed() when supported

toReversed() returns a new array in reverse order and leaves the input 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. The lib option controls which built-in declarations the compiler recognizes, but declarations do not add a method to the JavaScript runtime. Check both your TypeScript configuration and the runtime where the code will execute; a successful type check alone does not guarantee that toReversed() exists there. See the TypeScript TSConfig lib reference and MDN’s toReversed() documentation.

Copy first for broader runtime compatibility

Make a shallow copy, then reverse that copy:

const values = [1, 2, 3];
const reversed = [...values].reverse();
// Alternative: values.slice().reverse()

The spread and slice() forms leave the original array’s order intact. Choose the expression that matches your mutation needs: reverse() for an in-place change, and toReversed() or copy-then-reverse for a separate result.

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

Readonly arrays and shallow copies

A value typed as readonly T[] cannot be mutated through that readonly type, so calling reverse() on it is not allowed by TypeScript. Copy it first to get a mutable array, for example with [...items].reverse(). toReversed() is also non-mutating when its declaration and runtime support are available. TypeScript describes ReadonlyArray as a type for arrays that should not be changed in its Object Types handbook.

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

These copy-based approaches make a new outer array, not a deep copy of its elements. If the array contains objects, the original and reversed arrays still refer to the same objects. MDN explains this in its Array reference.

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

How sparse arrays behave

If an array contains empty slots, the methods handle them differently: reverse() preserves empty slots in their reversed positions, while toReversed() reads those holes as undefined and returns a dense array at those positions. For ordinary dense arrays of primitives, both approaches produce the expected reversed order. More detail is available in the MDN references for reverse() and toReversed().

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

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

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.