As long as you're dealing with simple ASCII characters, and you're happy to use built-in functions, this will work:

function reverse(s){
    return s.split("").reverse().join("");
}

If you need a solution that supports UTF-16 or other multi-byte characters, be aware that this function will give invalid unicode strings, or valid strings that look funny. You might want to consider this answer instead.

The array expansion operator is Unicode aware:

function reverse(s){
    return [...s].reverse().join("");
}

Another Unicode aware solution using split(), as explained on MDN, is to use a regexp with the u (Unicode) flag set as a separator.

function reverse(s){
    return s.split(/(?:)/u).reverse().join("");
}
Answer from belacqua on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › typescript › reverse-a-string-in-typescript
Reverse a String in TypeScript - GeeksforGeeks
July 23, 2025 - This method utilizes a loop to iterate through the characters of the string in reverse order, building the reversed string.
🌐
Mimo
mimo.org › tutorials › typescript › how-to-reverse-a-string-in-typescript
How to Reverse a String in TypeScript
Learn how to reverse a string in TypeScript using split, reverse, and join, plus Unicode-safe alternatives like Array.from().
🌐
Geodev
geodev.me › blog › reverse-string-typescript-string-literals
Reverse string using Typescript string literals - geodev
October 8, 2024 - type Reversed1 = Reverse<"TypeScript">; // "tpircSeptyT" type Reversed2 = Reverse<"Neptune">; // "enutpeN" type Reversed3 = Reverse<"Toyota">; // "atoyoT"
Top answer
1 of 16
1019

As long as you're dealing with simple ASCII characters, and you're happy to use built-in functions, this will work:

function reverse(s){
    return s.split("").reverse().join("");
}

If you need a solution that supports UTF-16 or other multi-byte characters, be aware that this function will give invalid unicode strings, or valid strings that look funny. You might want to consider this answer instead.

The array expansion operator is Unicode aware:

function reverse(s){
    return [...s].reverse().join("");
}

Another Unicode aware solution using split(), as explained on MDN, is to use a regexp with the u (Unicode) flag set as a separator.

function reverse(s){
    return s.split(/(?:)/u).reverse().join("");
}
2 of 16
440

The following technique (or similar) is commonly used to reverse a string in JavaScript:

// Don’t use this!
var naiveReverse = function(string) {
    return string.split('').reverse().join('');
}

In fact, all the answers posted so far are a variation of this pattern. However, there are some problems with this solution. For example:

naiveReverse('foo 𝌆 bar');
// → 'rab �� oof'
// Where did the `𝌆` symbol go? Whoops!

If you’re wondering why this happens, read up on JavaScript’s internal character encoding. (TL;DR: 𝌆 is an astral symbol, and JavaScript exposes it as two separate code units.)

But there’s more:

// To see which symbols are being used here, check:
// http://mothereff.in/js-escapes#1ma%C3%B1ana%20man%CC%83ana
naiveReverse('mañana mañana');
// → 'anãnam anañam'
// Wait, so now the tilde is applied to the `a` instead of the `n`? WAT.

A good string to test string reverse implementations is the following:

'foo 𝌆 bar mañana mañana'

Why? Because it contains an astral symbol (𝌆) (which are represented by surrogate pairs in JavaScript) and a combining mark (the in the last mañana actually consists of two symbols: U+006E LATIN SMALL LETTER N and U+0303 COMBINING TILDE).

The order in which surrogate pairs appear cannot be reversed, else the astral symbol won’t show up anymore in the ‘reversed’ string. That’s why you saw those �� marks in the output for the previous example.

Combining marks always get applied to the previous symbol, so you have to treat both the main symbol (U+006E LATIN SMALL LETTER N) as the combining mark (U+0303 COMBINING TILDE) as a whole. Reversing their order will cause the combining mark to be paired with another symbol in the string. That’s why the example output had instead of ñ.

Hopefully, this explains why all the answers posted so far are wrong.


To answer your initial question — how to [properly] reverse a string in JavaScript —, I’ve written a small JavaScript library that is capable of Unicode-aware string reversal. It doesn’t have any of the issues I just mentioned. The library is called Esrever; its code is on GitHub, and it works in pretty much any JavaScript environment. It comes with a shell utility/binary, so you can easily reverse strings from your terminal if you want.

var input = 'foo 𝌆 bar mañana mañana';
esrever.reverse(input);
// → 'anañam anañam rab 𝌆 oof'

As for the “in-place” part, see the other answers.

🌐
DEV Community
dev.to › __zamora__ › typescript-coding-chronicles-reverse-words-in-a-string-44no
Typescript Coding Chronicles: Reverse Words in a String - DEV Community
July 11, 2024 - Given an input string s, reverse the order of the words. A word is defined as a sequence of non-space characters. The words in s will be separated by at least one space.
🌐
CodeSignal
codesignal.com › learn › courses › practicing-string-operations-and-type-conversions-in-typescript › lessons › string-manipulation-in-typescript-reversing-words-within-a-string
String Manipulation in TypeScript: Reversing Words Within ...
In TypeScript, the split() method of the String object allows us to achieve this easily. The delimiter you'll use in the split() method is a single space " ". Here is a sample code to illustrate this: Note that " " as the delimiter ensures that the string is split at each space, effectively separating the words. ... Next, we need to reverse each word separated in the previous step.
🌐
JavaScript in Plain English
javascript.plainenglish.io › how-to-reverse-a-string-in-javascript-and-typescript-e9bc700eb28b
How to Reverse a String in JavaScript and TypeScript | by Sam C. Tomasi | JavaScript in Plain English
December 13, 2022 - DevAdvent 2022 How to Reverse a String in JavaScript and TypeScript Or, How To Use Array.map() And Array.reverse() Together To Modify A Sentence Today’s problem is how to reverse the order of the …
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › how-to-reverse-the-string-in-typescript
TypeScript - Array reverse()
January 3, 2023 - reverse() method reverses the element of an array. The first array element becomes the last and the last becomes the first. Returns the reversed single value of the array. On compiling, it will generate the same code in JavaScript.
🌐
Java Guides
javaguides.net › 2023 › 09 › typescript-reverse-string.html
TypeScript: Reverse a String
September 9, 2023 - we'll explore a simple method in TypeScript to reverse a string without using the built-in reverse() method.
🌐
Exercism
exercism.org › tracks › typescript › exercises › reverse-string
Reverse String in TypeScript on Exercism
Deep Dive into Reverse String! Explore 14 different ways to reverse a string, exploring a range of topics including Unicode Codepoints, Graphemes, Stack vs Heap allocations, and pointers.
🌐
CodeVsColor
codevscolor.com › 4 ways in typescript to reverse a string - codevscolor
4 ways in TypeScript to reverse a string - CodeVsColor
July 12, 2022 - Different ways in TypeScript to reverse a string. Learn how to reverse a string by using a for loop, using a while loop, by splitting the string and recursively in TypeScript.
🌐
GitBook
basarat.gitbook.io › algorithms › quiz › reverse-string
Reverse String | Algorithms in TypeScript
December 31, 2019 - Algorithms in TypeScript · Introduction · Getting Started · Basics · Data Structures · Shuffling · Sorting · Quiz · Reverse String · Palindrome · FizzBuzzPowered by GitBook · On this page · Copy · JavaScript string doesn't have a reverse method. But you can just use the Array.prototype.reverse method.
🌐
myCompiler
mycompiler.io › view › 3LLFndpMKct
Reverse String (TypeScript) - myCompiler
TypeScript 7.0.0 (native preview) Run Fork · Copy link Download Share on Facebook Share on Twitter Share on Reddit Embed on website · const message:string = "helloworld"; //one way with built in functions function reverseStringBuildIn(str){ var mystr = str.split("").reverse().join(""); return mystr; } function reverseStringWithLoop(str){ var mystr = ""; for(let i = str.length-1;i>=0;i--){ mystr += str[i]; } return mystr; } //Time Consuming Process if string is lengthy function reverseStringWithRecursion(str){ if(str ===""){ return str; }else{ return reverseStringWithRecursion(str.substr(1))+str.charAt(0); } } console.log("With Predefined Functions : "+message+" to "+ reverseStringBuildIn(message)); console.log("With Loop : "+message+" to " + reverseStringWithLoop(message)); console.log("With Recursion : "+message+" to " + reverseStringWithRecursion(message)); Output ·
🌐
Python Guides
pythonguides.com › reverse-string-in-typescript
How To Reverse A String In TypeScript? - Python Guides
July 1, 2025 - Learn how to reverse a string in TypeScript using various methods like split-reverse-join, for loops, reduce, and generics with real-world use cases.
🌐
Dirask
dirask.com › posts › TypeScript-reverse-string-pqeEnp
TypeScript - reverse string
function reverseString(text: string): string { const characters: string[] = text.split(''); const reversion: string[] = characters.reverse(); return reversion.join(''); } // Usage example: const text: string = 'This is my text...'; const reversion: ...
🌐
Exercism
exercism.org › tracks › typescript › exercises › reverse-string › solutions › reecen9696
reecen9696's solution for Reverse String in TypeScript on Exercism
See how @reecen9696 solved Reverse String in TypeScript and get inspired for how you could solve it too! Exercism is 100% free and a great way to level-up your programming skills in over 65 languages.
🌐
Reddit
reddit.com › r/learnprogramming › trying to return a reverse string in typescript, but execution keeps getting timed out. what am i doing wrong?
r/learnprogramming on Reddit: Trying to return a reverse string in TypeScript, but execution keeps getting timed out. What am I doing wrong?
December 7, 2020 -

So I got the following starter code. I need to complete the function that accepts a string parameter, and reverses each word in the string. All spaces in the string should be retained.

export function reverseWords(str: string): string {
  // your code here
  return "Go for it";
}

This is the approach I took:

I created a new variable newString that takes in an empty string. I then created a for loop where each iteration occurred backwards and then it spits the characters back out to newString.

export function reverseWords(str: string): string {
  var newString = "";
  
  for(var i = str.length - 1; i >=0; i++){
    newString += str[i];
  }
  return newString;
}

reverseWords("Hi. I am outside with your order");

When I run this code, the execution is timed out. Can someone help me and explain what may be happening? I am trying to avoid using built-in methods and functions.