To reverse a string in JavaScript, you can use the built-in split(), reverse(), and join() methods together. Example:
let str = "hello";
let reversed = str.split("").reverse().join("");
console.log(reversed); // Output: "olleh"
This is the simplest and most common way to reverse a string.
Reversing a string is one of the most popular JavaScript interview questions and a great exercise to understand arrays, strings, and built-in methods. It’s also a common task in algorithms and text-processing applications.
Key Takeaways of Reserve a String Javascript
- split() → Converts string to array
- reverse() → Reverses the array
- join() → Joins array back into string
- Alternative methods → For loops, recursion, or ES6 spread syntax

What Is String Reversal in JavaScript?
String reversal means rearranging the characters of a string in the opposite order — for example, turning "hello" into "olleh".
This concept is widely used in data manipulation, encryption algorithms, and palindrome checking.
Since strings in JavaScript are immutable (they can’t be changed directly), we need to use array or loop-based approaches to achieve reversal.
How to Reverse a String Using Built-in Methods
The easiest and most common way to reverse a string in JavaScript is by combining three built-in methods:split(), reverse(), and join().
Here’s how it works step by step:
let str = "hello";
let reversed = str.split("").reverse().join("");
console.log(reversed); // Output: "olleh"
Step-by-step explanation:
- split(“”) → Converts the string into an array of characters →
["h","e","l","l","o"] - reverse() → Reverses the array order →
["o","l","l","e","h"] - join(“”) → Joins the array back into a single string →
"olleh"
This method is short, clean, and perfect for quick string reversals.
Alternative Methods to Reverse a String
1. Using a for loop
A manual approach that iterates over the string from the end to the beginning:
let str = "hello";
let reversed = "";
for (let i = str.length - 1; i >= 0; i--) {
reversed += str[i];
}
console.log(reversed); // "olleh"
This gives more control and helps beginners understand how string traversal works.
2. Using recursion
You can also reverse a string using a recursive function:
function reverseString(str) {
if (str === "") return "";
return reverseString(str.substr(1)) + str[0];
}
console.log(reverseString("hello")); // "olleh"
Here, the function keeps slicing the string and stacking characters backward until it reaches an empty string.
3. Using spread operator and reverse()
ES6 introduced the spread operator (...), which can make code even shorter:
let str = "hello";
let reversed = [...str].reverse().join("");
console.log(reversed); // "olleh"
It works just like split(""), but looks cleaner and modern.
When Should You Use Each Method?
| Method | Best For | Performance | Ease of Use |
|---|---|---|---|
| split() + reverse() + join() | Everyday use, quick scripts | Good | Very Easy |
| for loop | Teaching, manual control | Excellent | Moderate |
| recursion | Learning algorithms | Poor for large strings | Moderate |
| spread operator | Modern ES6 code | Good | Easy |
Tip: For most use cases, stick with split().reverse().join() — it’s clean, efficient, and readable.
Pros & Cons of Reverse a String in JavaScript
| Method | Pros | Cons |
|---|---|---|
| split-reverse-join | Simple, one-liner | Uses extra memory |
| Loop | More control | Slightly longer code |
| Recursion | Elegant | Less efficient for long strings |
Practical Uses for Reversing a String in JavaScript
- Search Engine Optimization (SEO): Companies like Google implement string-reversing techniques to help build algorithms for natural language processing, particularly when identifying and indexing keywords. Here’s a simple code snippet to reverse a string for such purposes:
Output: `elgooG`function reverseString(str) {
return str.split("").reverse().join("");
}
console.log(reverseString("Google")); - Data Security and Encryption: Companies such as Amazon may use string reversal as part of their encryption techniques. An introductory piece of code for understanding the concept is as follows:
Output: `ataDterceS`
function reverseForEncryption(str) {
return str.split("").reverse().join("");
}
console.log(reverseForEncryption("SecretData")); - User-Interface Animation: Applications like Facebook could use string manipulation in animations where strings are reversed to create visual effects. Consider this simple example:
function animateReverseString(str) {
return str.split("").reverse().join("");
}
console.log(animateReverseString("Like"));
Output: `ekiL` - Database Query Optimization: Companies like Spotify might reverse strings to optimize search queries by matching reversed indexes more efficiently. A basic demonstration is:
function optimizeQuery(str) {
return str.split("").reverse().join("");
}
console.log(optimizeQuery("Music"));
Output: `cisuM`
Reverse a String in JavaScript: Queries
So, you’re eager to tackle reversing a string in JavaScript, but you’re stuck with questions that’ve likely crossed your mind? Let’s dive into some of these queries that aren’t always covered in typical tutorials!
- How do you reverse a string with built-in JavaScript methods?
One of the simplest ways is using JavaScript’s handy methods like `split()`, `reverse()`, and `join()`. Here’s how you can do it:function reverseString(str) {
return str.split('').reverse().join('');
}
console.log(reverseString("hello")); // Output: "olleh" - Can you reverse a string without using built-in methods?
Certainly! You can loop through the string from the end to the beginning and construct a new string:function reverseStringManually(str) {
let reversed = '';
for (let i = str.length - 1; i >= 0; i--) {
reversed += str[i];
}
return reversed;
}
console.log(reverseStringManually("world")); // Output: "dlrow" - Why might you choose recursion to reverse a string?
Recursion is a neat technique to break down the problem into smaller sub-problems, although not always the most efficient. Check this out:function reverseStringRecursive(str) {
if (str === "") return "";
else return reverseStringRecursive(str.substr(1)) + str.charAt(0);
}
console.log(reverseStringRecursive("javascript")); // Output: "tpircsavaj" - How can I reverse words in a sentence, but not the whole string?
This can be done by splitting the string by spaces, reversing each word, and joining them back:function reverseWordsInSentence(sentence) {
return sentence.split(' ').map(word => word.split('').reverse().join('')).join(' ');
}
console.log(reverseWordsInSentence("hello world")); // Output: "olleh dlrow" - Can I use destructuring to help in reversing a string?
Destructuring assignment can be creatively used with the spread operator:const reverseStringDestructuring = (str) => [...str].reverse().join('');
console.log(reverseStringDestructuring("destructuring")); // Output: "gnirutcurtsed" - What’s a simple interview-worthy approach to reverse a string in-place?
While JavaScript strings are immutable, using an array can simulate an “in-place” reversal:function reverseStringInPlace(str) {
let arr = str.split('');
let left = 0;
let right = arr.length - 1;
while (left < right) {
[arr[left], arr[right]] = [arr[right], arr[left]];
left++;
right--;
}
return arr.join('');
}
console.log(reverseStringInPlace("interview")); // Output: "weivretni" - Does reversing a string affect performance if the string is large?
Reversing strings can be costly in terms of performance if they’re massive due to memory allocation, use methods wisely! - How do I handle white spaces while reversing a string?
To keep leading/trailing spaces, use the regular reverse. If you want to trim them before reversal, apply `trim()` first:function reverseStringWithTrim(str) {
return str.trim().split('').reverse().join('');
}
console.log(reverseStringWithTrim(" space ")); // Output: "ecaps"
Discover our AI-powered js online compiler, where you can instantly write, run, and test your code. Our intelligent compiler simplifies your coding experience, offering real-time suggestions and debugging tips, streamlining your journey from coding novice to expert with ease. Explore it today and boost your coding skills!
Conclusion
“Reverse a String in JavaScript” helps you understand string manipulation, a fundamental skill in any programmer’s toolkit. Tackling this challenge boosts your confidence, encouraging further exploration of coding. Ready for more insights? Visit Newtum to discover Java, Python, C, C++, and more!
Edited and Compiled by
This article was compiled and edited by @rasikadeshpande, who has over 4 years of experience in writing. She’s passionate about helping beginners understand technical topics in a more interactive way.
FAQ about Reverse a String in JavaScript
Can you reverse a string without using built-in functions?
Yes, you can use a simple for loop to iterate from the end of the string and build a new reversed string manually.