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
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › reverse
Array.prototype.reverse() - JavaScript - MDN Web Docs
The reverse() method is generic. It only expects the this value to have a length property and integer-keyed properties. Although strings are also array-like, this method is not suitable to be applied on them, as strings are immutable.
🌐
freeCodeCamp
freecodecamp.org › news › how-to-reverse-a-string-in-javascript-in-3-different-ways-75e4763c68cb
Three Ways to Reverse a String in JavaScript
March 14, 2016 - The split() method splits a String object into an array of string by separating the string into sub strings. The reverse() method reverses an array in place.
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.

🌐
SamanthaMing
samanthaming.com › pictorials › how-to-reverse-a-string
How to Reverse a String in JavaScript | SamanthaMing.com
Split our string into an array of letters. ... Excellent, now that we have an array of letters. We can call our built-in array method to reverse the order.
🌐
Medium
medium.com › free-code-camp › how-to-reverse-a-string-in-javascript-in-3-different-ways-75e4763c68cb
Three Ways to Reverse a String in JavaScript | by Sonya Moisset | We’ve moved to freeCodeCamp.org/news | Medium
February 16, 2017 - The split() method splits a String object into an array of string by separating the string into sub strings. The reverse() method reverses an array in place.
🌐
GitHub
github.com › mdn › content › blob › main › files › en-us › web › javascript › reference › global_objects › array › reverse › index.md
content/files/en-us/web/javascript/reference/global_objects/array/reverse/index.md at main · mdn/content
The `reverse()` method is [generic](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#generic_array_methods). It only expects the `this` value to have a `length` property and integer-keyed properties. Although strings are also array-like, this method is not suitable to be applied on them, as strings are immutable.
Author   mdn
Find elsewhere
🌐
Medium
medium.com › sonyamoisset › reverse-a-string-in-javascript-a18027b8e91c
Reverse a String in JavaScript. This article is based on Free Code Camp… | by Sonya Moisset | iDevOI | Medium
November 25, 2016 - The split() method splits a String object into an array of string by separating the string into sub strings. The reverse() method reverses an array in place.
🌐
Ducky Obrien
duckyobrien.com › 2022 › 09 › 02 › how-to-reverse-a-string-in-javascript
How to reverse a string in JavaScript – Ducky Obrien
December 21, 2022 - For starters, strings in JavaScript behave like arrays but are not actually arrays so handy array methods like reverse() cannot be used on them. So we use the split method for string objects to convert the string into an array. This is the split function according to MDN: “The split() method takes a pattern and divides a String into an ordered list of substrings by searching for the pattern, puts these substrings into an array, and returns the array.”
🌐
Medium
medium.com › @samanthaming › web-basics-how-to-reverse-a-string-in-javascript-bdb1e31fbc18
Web Basics: How to Reverse a String in JavaScript | by Samantha Ming | Medium
October 26, 2018 - So the output is an array of each letter. const name = 'samantha'; const result = name.split('');console.log(result); // [ 's', 'a', 'm', 'a', 'n', 't', 'h', 'a' ] This method is used to reverse the order of the elements in an array.
🌐
Sqlhabit
sqlhabit.com › mdn › reverse
SQL Habit | reverse() function in SQL
The reverse() function in SQL flips the order of characters in a string, producing a mirror image of the original text.
🌐
DEV Community
dev.to › uyaiabasi › how-to-reverse-string-in-javascript-ppj
How to Reverse string in JavaScript - DEV Community
March 27, 2023 - Using the split(), reverse(), and join() methods This is the most commonly used method to reverse a string in JavaScript. It involves splitting the string into an array of characters, reversing the array, and then joining the array back into ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › toReversed
Array.prototype.toReversed() - JavaScript - MDN Web Docs
July 10, 2025 - The toReversed() method transposes the elements of the calling array object in reverse order and returns a new array.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › TypedArray › reverse
TypedArray.prototype.reverse() - JavaScript - MDN Web Docs
July 10, 2025 - The reverse() method of TypedArray instances reverses a typed array in place and returns the reference to the same typed array, the first typed array element now becoming the last, and the last typed array element becoming the first. In other words, elements order in the typed array will be ...
🌐
Stack Abuse
stackabuse.com › how-to-reverse-a-string-in-javascript
How to Reverse a String in JavaScript
September 28, 2023 - String indices are like array indices, they start at 0. Therefore, we started the loop from one less than the length of the string (picking up the letter r) and loop till our counter goes to 0 (with the letter g). The reversedStr variable concatenates the elements of the string.
🌐
Reddit
reddit.com › r/javascript › how to reverse a string in javascript in 3 different ways ?
r/javascript on Reddit: How to Reverse a String in JavaScript in 3 Different Ways ?
March 16, 2016 - From Array.prototype.reverse on MDN: The reverse() method reverses an array in place · Since strings are immutable in JS, an engine throws an error. More replies · dont_forget_canada · • 10y ago · this is horrifying.
🌐
AlgoCademy
algocademy.com › link
Reverse Words in a String - JavaScript
In this blog post, we discussed how to reverse the words in a string using JavaScript. We covered the problem definition, approach, algorithm, code implementation, complexity analysis, edge cases, and testing. Understanding and solving such problems is crucial for improving your coding skills and preparing for technical interviews. Keep practicing and exploring further to enhance your problem-solving abilities. For further reading and practice, consider the following resources: MDN Web Docs: String.prototype.split() MDN Web Docs: Array.prototype.reverse() MDN Web Docs: Array.prototype.join() LeetCode: Practice coding problems ·
🌐
DEV Community
dev.to › uyaiabasi › how-to-reverse-string-in-javascript-ppj › comments
[Discussion] How to Reverse string in JavaScript — DEV Community
March 27, 2023 - function reverseString(str) { return Array.from(str).reverse().join(''); } ... It returns an iterator that yields the Unicode code points of the string value as individual strings.