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 a String in TypeScript

Use `split("").reverse().join("")` for a basic TypeScript string reversal, but know that it reverses UTF-16 code units and may split Unicode characters.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For a basic reversal, use split("").reverse().join(""):

function reverseString(value: string): string {
  return value.split("").reverse().join("");
}

This reverses UTF-16 code units, which is fine for many ordinary strings but can split some Unicode characters. If that matters, first decide whether you need to reverse code points or user-perceived characters.

What the code does

split("") turns the string into an array of UTF-16 code units, reverse() reverses the array, and join("") combines the units into a new string. The TypeScript handbook’s Basic Types page demonstrates this pattern as an official example.

TypeScript checks the code, but the reversal is performed by standard JavaScript string and array methods at runtime. TypeScript is a static typechecker for JavaScript, not a language feature that supplies a separate string-reversal operation; see the Handbook introduction.

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

Why the basic method can break some Unicode characters

Some characters are encoded as multiple UTF-16 code units. For example, TypeScript 1.5’s notes explain that 𠮷 is represented by the surrogate pair 0xD842 and 0xDFB7. Reversing the individual units can separate that pair, producing text that looks corrupted rather than reversing the character intact. See the TypeScript 1.5 release notes.

There are different possible meanings of “reverse”:

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
  • UTF-16 code units: what the basic split("").reverse().join("") implementation reverses.
  • Unicode code points: a code-point iteration approach can keep surrogate pairs together, but may still separate marks from their base letters or split multi-code-point emoji.
  • User-perceived grapheme clusters: the visible characters a person may regard as single units. For this requirement, choose and verify a grapheme-aware approach suited to your runtime; the basic snippet and code-point iteration alone do not guarantee this behavior.

Use the right TypeScript type

Declare parameters and return values with the primitive string type, as in the example. The handbook’s Basic Types page contrasts it with the String wrapper type and recommends the lowercase primitive.

Check compatibility when using newer APIs

If you choose an alternative that relies on newer JavaScript APIs, check both the project’s TypeScript library declarations and the JavaScript runtime where the code will execute. The TSConfig reference for lib explains how library declarations correspond to platform APIs. A declaration lets TypeScript understand an API; it does not add that API to the runtime or provide a polyfill.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Runtime reversal is different from a type-level transformation

Template literal type intrinsics are for type-level string transformations, not for reversing a runtime string value. Their documented manipulations use JavaScript runtime string functions and are not locale-aware; see the template literal types documentation.

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 *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

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
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

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.