Introduction
A reverse string is a string whose characters are in reverse order.
Reversing a string in JavaScript involves creating a new string with the original string’s characters in the opposite order. This can be achieved by converting the string to an array, reversing it, and then joining it back into a string.
For example:
Original String: Hello
Reversed String: olleH
What is JavaScript Reverse String?
JavaScript reverse string means converting a string into the opposite order of characters.
Example:
"JavaScript";
Reversed string:
"tpircSavaJ";
Method 1: Reverse String Using split(), reverse(), join()
This is the most popular method.
string.split("").reverse().join("")
Example:
let text = "John";
let result = text.split("").reverse().join("");
console.log(result);
Output:
Explanation:
1. split:
Converts a string into an array.
"John".split("")
Output:
2. reverse()
Reverses array elements.
Output:
3. join(“”)
Converts the array back into a string.
Output:
Method 2: Reverse String Using a for Loop
Example:
let text = "JavaScript";
let reversed = "";
for(let i = text.length - 1; i >= 0; i--){
reversed += text[i];
}
console.log(reversed);
Output:
Explanation:
Loop starts from the last character and adds characters one by one.
Method 3: Reverse String Using a while Loop
let str = "Coding";
let reversed = "";
let i = str.length - 1;
while(i >= 0){
reversed += str[i];
i--;
}
console.log(reversed);
Output:
Method 4: Reverse String Using Recursion
Recursion means a function calling itself.
Example:
function reverseString(str){
if(str === "")
return "";
return reverseString(str.substr(1))
+ str.charAt(0);
}
console.log(reverseString("Hello"));
Output:
Method 5: Reverse String Using reduce()
Example:
let text = "Java";
let reversed =
text.split("")
.reduce((rev,char)=>char+rev,"");
console.log(reversed);
Output:
Method 6: Reverse String Using for…of Loop
Example:
let str = "World";
let reverse = "";
for(let char of str){
reverse = char + reverse;
}
console.log(reverse);
Output:
Reverse Words Instead of Characters
Sometimes you need to reverse words.
Original:
I am John
Reversed:
John am I
Example:
let text = "I am John";
let result = text.split(" ").reverse().join(" ");
console.log(result);
Output:
Reverse Number Converted into String
When you have to reverse a number.
Example:
let number = 12345;
let result = number.toString().split("").reverse().join("");
console.log(result);
Output:
Common Mistakes While Reversing a String
You will see some common mistakes
Mistake 1: Forgetting to join()
Wrong:
name = "John";
let result = name.split("").reverse();
console.log(result);
Output:
Returns an array instead of a string.
Correct:
name = "John";
let result = name.split("").reverse().join();
console.log(result);
Output:
Mistake 2: Using reverse() Directly on String
Wrong:
name = "John";
let result = name.reverse();
console.log(result);
JavaScript Reverse String – Interview Questions
Q 1: How to reverse a string?
Q 2: Can strings be reversed directly?
Q 3: Method used to split the string?
Q 4: Method to reverse an array?
Q 5: Method to join an array?
JavaScript Reverse String – Objective Questions (MCQs)
Q1. Which method splits a string into an array?
Q2. Which array method reverses elements?
Q3. Which method joins array elements into a string?
Q4. What is the output of: "abc".split("").reverse().join("")
Q5. Which data type is returned after reversing a string?
Conclusion
JavaScript reverse string is a common operation used in interviews, algorithms, and real-world applications. Since strings do not support the reverse() method directly, developers usually convert strings into arrays using split(), reverse them using reverse(), and then convert back using join().
You can also reverse strings using loops, recursion, and reduce() functions. Learning multiple approaches improves problem-solving skills and helps in coding interviews.