0
0
JavascriptProgramBeginner · 2 min read

JavaScript Program to Reverse String Using Recursion

You can reverse a string in JavaScript using recursion by defining a function like function reverseString(str) { if (str === "") return ""; return reverseString(str.slice(1)) + str[0]; } which calls itself with the string minus the first character and adds the first character at the end.
📋

Examples

Input"hello"
Output"olleh"
Input"a"
Output"a"
Input""
Output""
🧠

How to Think About It

To reverse a string using recursion, think of taking the first character off and then reversing the rest of the string. Then, add the first character to the end of the reversed smaller string. Keep doing this until the string is empty, which is the stopping point.
📐

Algorithm

1
Get the input string.
2
Check if the string is empty; if yes, return an empty string.
3
Call the function recursively with the string excluding the first character.
4
Add the first character of the current string to the end of the result from the recursive call.
5
Return the combined string.
💻

Code

javascript
function reverseString(str) {
  if (str === "") {
    return "";
  }
  return reverseString(str.slice(1)) + str[0];
}

console.log(reverseString("hello"));
Output
olleh
🔍

Dry Run

Let's trace reversing the string "abc" through the code.

1

Initial call

reverseString("abc") calls reverseString("bc") and will add 'a' later.

2

Second call

reverseString("bc") calls reverseString("c") and will add 'b' later.

3

Third call

reverseString("c") calls reverseString("") and will add 'c' later.

4

Base case

reverseString("") returns "" because the string is empty.

5

Returning from third call

reverseString("c") returns "" + 'c' = "c".

6

Returning from second call

reverseString("bc") returns "c" + 'b' = "cb".

7

Returning from initial call

reverseString("abc") returns "cb" + 'a' = "cba".

CallInput StringReturned Value
1"abc"reverseString("bc") + 'a'
2"bc"reverseString("c") + 'b'
3"c"reverseString("") + 'c'
4"""" (base case)
3"c""c"
2"bc""cb"
1"abc""cba"
💡

Why This Works

Step 1: Base case stops recursion

When the string is empty (""), the function returns an empty string to stop further calls.

Step 2: Recursive call reduces problem size

The function calls itself with the string minus the first character (str.slice(1)), making the problem smaller each time.

Step 3: Building reversed string on return

After the recursive call returns, the first character of the current string (str[0]) is added to the end, gradually building the reversed string.

🔄

Alternative Approaches

Using a loop
javascript
function reverseStringLoop(str) {
  let reversed = "";
  for (let i = str.length - 1; i >= 0; i--) {
    reversed += str[i];
  }
  return reversed;
}

console.log(reverseStringLoop("hello"));
This method uses a simple loop and is usually faster and uses less memory than recursion.
Using built-in functions
javascript
function reverseStringBuiltIn(str) {
  return str.split("").reverse().join("");
}

console.log(reverseStringBuiltIn("hello"));
This method is very concise and uses JavaScript's built-in array methods for simplicity.

Complexity: O(n) time, O(n) space

Time Complexity

The function calls itself once per character, so it runs in linear time relative to the string length.

Space Complexity

Each recursive call adds a new layer to the call stack, so space used grows linearly with string length.

Which Approach is Fastest?

The loop and built-in methods are faster and use less memory than recursion because they avoid call stack overhead.

ApproachTimeSpaceBest For
RecursionO(n)O(n)Learning recursion and problems naturally recursive
LoopO(n)O(1)Efficient string reversal in practice
Built-in functionsO(n)O(n)Quick and concise code
💡
Use recursion only for learning or when the problem naturally fits recursive thinking; loops are often more efficient for string reversal.
⚠️
Beginners often forget the base case, causing infinite recursion and crashing the program.