🌐
Code Beautify
codebeautify.org › reverse-string
Best Reverse String / Text tool
Reverse String / Text Online easy to use tool to reverse your string, paste your string or load from url or Open a File and share
🌐
Browserling
browserling.com › tools › text-reverse
Reverse Text - Reverse String - Online - Browserling Web Developer Tools
World's simplest online text and string reverser for web developers and programmers. Just paste your data in the form below, press the Reverse button, and you'll get your input reversed. Press a button – get the reversed data.
🌐
Online String Tools
onlinestringtools.com › reverse-string
Reverse a String – Online String Tools
Simple, free and easy to use online tool that reverses strings. No intrusive ads, popups or nonsense, just a string reverser. Load a string and reverse it.
🌐
Eternitech
eternitech.com › online-tools › string-utilities
Free Online String Utilities | Reverse String Online
April 12, 2023 - You don’t need to download anything, and there are no popups, advertising, or other unnecessary distractions. Just copy, paste and escape. 1Write or directly paste your string into the big rectangular input box which says “Enter/paste string” · 2After pasting, you can easily convert input string into lowercase, uppercase, or reverse.
🌐
Onecompiler
tools.onecompiler.com › reverse-string
Reverse string online
A simple, efficient and free online tool to quickly reverse the given string.
🌐
Convert String
convertstring.com › StringFunction › ReverseString
Reverse String - Online String Reverser
Reverse string. Online tool for reversing the order of the characters within a string. Flip a string using this free online string reversing utility.
🌐
IPVoid
ipvoid.com › reverse-string
Reverse a String or Text Online | IPVoid
This online tool lets you reverse a string or text online, supports unicode characters. You can use this tool to quickly reverse the order of the characters within a string.
🌐
TextFixer
textfixer.com › tools › reverse-text-generator.php
Reverse Text Generator
Use this text reverser to quickly reverse text online. This free generator tool can also convert your text message into a backwards text version of your words.
Find elsewhere
🌐
Cloudzenia
cloudzenia.com › tools › string-tools › reverse-string
Reverse String - Reverse Your Text Online Instantly | CloudZenia
Reverse your text instantly with Reverse String tools. Easily flip any string of text for fun, puzzles, or creative projects online - CloudZenia Tools
🌐
Text-Utils
text-utils.com › reverse a string
Reverse a String - Online Text Tools | Text-Utils.com
April 2, 2021 - The Online Reverse String tool can be used to quickly reverse a string input.
Top answer
1 of 16
1016

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.

🌐
Text Reverser
textreverse.com
Text Reverser (Reverse Text) - Backwards Text Generator
Text Reverser (reverse text generator) used to reverse words, spells and letters. It is free online backwards text generator tool.
🌐
FreeFormatter
freeformatter.com › string-utilities.html
Free Online String Utilities - FreeFormatter.com
Free online string utilities such as convert to lowercase, convert to uppercase, word count, character count, string splitter, reverse and more.
🌐
LeetCode
leetcode.com › problems › reverse-string
Reverse String - LeetCode
The input string is given as an array of characters s. You must do this by modifying the input array in-place [https://en.wikipedia.org/wiki/In-place_algorithm] with O(1) extra memory.
🌐
Better Converter
better-converter.com › Reverse-String
Reverse String - Online Converter
A tool to reverse a string, or write it backwards, e.g. for testing palindrome texts.
🌐
Browserling
browserling.com › tools › reverse-lines
Reverse Text Lines - Flip Line Order - Online - Browserling Web Developer Tools
World's simplest online line order reverser for web developers and programmers. Just paste your text in the form below, press the Reverse Lines button, and you'll get reversed lines. Press a button – get the result.
🌐
Online String Tools
onlinestringtools.com › invert-string-case
Invert Letter Case in a String – Online String Tools
Simple, free and easy to use online tool that inverts letter case in a string. No intrusive ads, popups or nonsense, just a string case inverter. Load a string, get its letters inverted.
🌐
IPLocation
iplocation.io › reverse-string
Free Reverse Text Generator - Backwards Text Generator
Create reverse text and strings easily with the help of our Text Reverser. Enter your text, press the button, and get your backward text in seconds.
🌐
Coders Tool
coderstool.com › reverse-string
Reverse String Online | Instant Text Reverser
This tool is perfect for developers and anyone working with text data, as it supports a wide range of features beyond basic reversal. You can trim whitespace characters from your input string, skip empty lines when working in multi-line mode, and preserve the original formatting of your text. The online reverse string tool is vital for handling strings in various formats, including uppercase, lowercase, and title case, ensuring your reversed string matches your formatting needs.
Top answer
1 of 14
3161

Using slicing:

>>> 'hello world'[::-1]
'dlrow olleh'

Slice notation takes the form [start:stop:step]. In this case, we omit the start and stop positions since we want the whole string. We also use step = -1, which means, "repeatedly step from right to left by 1 character".

2 of 14
329

What is the best way of implementing a reverse function for strings?

My own experience with this question is academic. However, if you're a pro looking for the quick answer, use a slice that steps by -1:

>>> 'a string'[::-1]
'gnirts a'

or more readably (but slower due to the method name lookups and the fact that join forms a list when given an iterator), str.join:

>>> ''.join(reversed('a string'))
'gnirts a'

or for readability and reusability, put the slice in a function

def reversed_string(a_string):
    return a_string[::-1]

and then:

>>> reversed_string('a_string')
'gnirts_a'

Longer explanation

If you're interested in the academic exposition, please keep reading.

There is no built-in reverse function in Python's str object.

Here is a couple of things about Python's strings you should know:

  1. In Python, strings are immutable. Changing a string does not modify the string. It creates a new one.

  2. Strings are sliceable. Slicing a string gives you a new string from one point in the string, backwards or forwards, to another point, by given increments. They take slice notation or a slice object in a subscript:

    string[subscript]
    

The subscript creates a slice by including a colon within the braces:

    string[start:stop:step]

To create a slice outside of the braces, you'll need to create a slice object:

    slice_obj = slice(start, stop, step)
    string[slice_obj]

A readable approach:

While ''.join(reversed('foo')) is readable, it requires calling a string method, str.join, on another called function, which can be rather relatively slow. Let's put this in a function - we'll come back to it:

def reverse_string_readable_answer(string):
    return ''.join(reversed(string))

Most performant approach:

Much faster is using a reverse slice:

'foo'[::-1]

But how can we make this more readable and understandable to someone less familiar with slices or the intent of the original author? Let's create a slice object outside of the subscript notation, give it a descriptive name, and pass it to the subscript notation.

start = stop = None
step = -1
reverse_slice = slice(start, stop, step)
'foo'[reverse_slice]

Implement as Function

To actually implement this as a function, I think it is semantically clear enough to simply use a descriptive name:

def reversed_string(a_string):
    return a_string[::-1]

And usage is simply:

reversed_string('foo')

What your teacher probably wants:

If you have an instructor, they probably want you to start with an empty string, and build up a new string from the old one. You can do this with pure syntax and literals using a while loop:

def reverse_a_string_slowly(a_string):
    new_string = ''
    index = len(a_string)
    while index:
        index -= 1                    # index = index - 1
        new_string += a_string[index] # new_string = new_string + character
    return new_string

This is theoretically bad because, remember, strings are immutable - so every time where it looks like you're appending a character onto your new_string, it's theoretically creating a new string every time! However, CPython knows how to optimize this in certain cases, of which this trivial case is one.

Best Practice

Theoretically better is to collect your substrings in a list, and join them later:

def reverse_a_string_more_slowly(a_string):
    new_strings = []
    index = len(a_string)
    while index:
        index -= 1                       
        new_strings.append(a_string[index])
    return ''.join(new_strings)

However, as we will see in the timings below for CPython, this actually takes longer, because CPython can optimize the string concatenation.

Timings

Here are the timings:

>>> a_string = 'amanaplanacanalpanama' * 10
>>> min(timeit.repeat(lambda: reverse_string_readable_answer(a_string)))
10.38789987564087
>>> min(timeit.repeat(lambda: reversed_string(a_string)))
0.6622700691223145
>>> min(timeit.repeat(lambda: reverse_a_string_slowly(a_string)))
25.756799936294556
>>> min(timeit.repeat(lambda: reverse_a_string_more_slowly(a_string)))
38.73570013046265

CPython optimizes string concatenation, whereas other implementations may not:

... do not rely on CPython's efficient implementation of in-place string concatenation for statements in the form a += b or a = a + b . This optimization is fragile even in CPython (it only works for some types) and isn't present at all in implementations that don't use refcounting. In performance sensitive parts of the library, the ''.join() form should be used instead. This will ensure that concatenation occurs in linear time across various implementations.