JavaScript Reverse String

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:

nhoJ

Explanation:

1. split:

Converts a string into an array.


"John".split("")

Output:

[“J”, “o”, “h”, “n”]

2. reverse()

Reverses array elements.

Output:

[“n”, “h”, “o”, “J”]

3. join(“”)

Converts the array back into a string.

Output:

“nhoJ”

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:

tpircSavaJ

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:

gnidoC

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:

olleH

Method 5: Reverse String Using reduce()

Example:


let text = "Java";

let reversed =
text.split("")
.reduce((rev,char)=>char+rev,"");

console.log(reversed);

Output:

avaJ

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:

dlroW

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:

John am I

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:

54321

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:

[‘J’,’o’,’h’,’n’]

Returns an array instead of a string.

Correct:


name = "John";
let result = name.split("").reverse().join();
console.log(result);

Output:

nhoJ

Mistake 2: Using reverse() Directly on String

Wrong:


name = "John";
let result = name.reverse();
console.log(result);
Error

JavaScript Reverse String – Interview Questions

Q 1: How to reverse a string?
Ans: Convert to an array, reverse, and join.
Q 2: Can strings be reversed directly?
Ans: No, they are immutable.
Q 3: Method used to split the string?
Ans: split("").
Q 4: Method to reverse an array?
Ans: reverse().
Q 5: Method to join an array?
Ans: join("").

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.