Share this article

Improve this guide

How to reverse a string in JavaScript [Step-by-step]

6 min. read

Updated onOctober 4, 2023

updated onOctober 4, 2023

Share this article

Improve this guide

Read our disclosure page to find out how can you help Windows Report sustain the editorial teamRead more

Key notes

A palindrome is a word or list of characters that read the same when reversed. A good example of this is the word ‘RADAR’.

The easiest way to check for a palindrome inJavaScriptis to create a copy of the original string, reverse it, and then compare it. If you want to build an algorithm that does this effectively, you’ll need to learn how to reverse a string inJavaScript. In this guide, you’ll learn how.

How do I reverse a string with JavaScript?

How do I reverse a string with JavaScript?

1. Convert it into an array and reverse it

The best way to implement the above steps is through a function. Here’s a quick source code example that you can copy, test, and modify:

In the above example, we create a function that reverses a hard-coded string and then sets a document element to it.  The function is then called from a button labeled ‘Reverse’.

There are numerous ways you can shorten and modify this code. For instance, instead of assigning the joined string to thestrvariable, you can reassign it to to theresvariableand then point the  demo HTML document element to it i.e.

res = res.join("");document.getElementById(“demo”).innerHTML = res;

Alternatively, you can condense all three steps into a single line:

var res = str.split("").reverse().join("");

Either way, the results should be the same:

Want to learn JavaScript even faster? Learn why you should download JavaScript Code by reading this article!

2. Reverse the string in JavaScript using a for loop

Once again, you should apply the above operation in a function within scripting tags. Whether you want to include theJavaScriptas an external file or embedded code is up to you. Here’s how the above solution looks in action:

The above code works by looping through your original string in reverse and clones it character by character. If you’re not interested in explicitly casting your string into an array, then you may find this solution more suited to your goals.

3. Reverse the string in JavaScript using recursion

You should call this method from another function that’s responsible for displaying the results. The complete code should look something like this:

Recursion works a lot like a for loop, but you’re trickingJavaScript‘s logic to work for you. Instead of appending characters to the end of the string, it attaches it to the front with each loop. Nevertheless, the results are much the same.

The above examples can be tested by using Windows Notepad and a web browser. However, we suggest using across-platform code editoror an integrated development environment to save yourself some time.

How to reverse a string in aJavascriptis a common interview question. Therefore, understanding it may be imperative to your success as a software developer.

Nonetheless, let us know which method worked best for you in the comments below. As always, thank you for reading.

[wl_navigator]

More about the topics:Javascript

Milan Stanojevic

Windows Toubleshooting Expert

Milan has been enthusiastic about technology ever since his childhood days, and this led him to take interest in all PC-related technologies. He’s a PC enthusiast and he spends most of his time learning about computers and technology.

Before joining WindowsReport, he worked as a front-end web developer. Now, he’s one of the Troubleshooting experts in our worldwide team, specializing in Windows errors & software issues.

User forum

0 messages

Sort by:LatestOldestMost Votes

Comment*

Name*

Email*

Commenting as.Not you?

Save information for future comments

Comment

Δ

Milan Stanojevic

Windows Toubleshooting Expert

Before joining WindowsReport, he worked as a front-end web developer. Now, he’s specialized in Windows errors & software issues.