0
0
JavascriptProgramBeginner · 2 min read

JavaScript Program to Check Palindrome Using Recursion

You can check if a string is a palindrome using recursion in JavaScript by defining a function like function isPalindrome(str) { if (str.length <= 1) return true; if (str[0] !== str[str.length - 1]) return false; return isPalindrome(str.slice(1, -1)); } which compares the first and last characters and calls itself on the middle substring.
📋

Examples

Input"racecar"
Outputtrue
Input"hello"
Outputfalse
Input"a"
Outputtrue
🧠

How to Think About It

To check if a string is a palindrome using recursion, think about comparing the first and last characters. If they are the same, then check the substring inside them by calling the same check again. If at any point the characters don't match, it's not a palindrome. If the string is empty or has one character, it is a palindrome by definition.
📐

Algorithm

1
Get the input string.
2
If the string length is 0 or 1, return true because it's a palindrome.
3
Compare the first and last characters of the string.
4
If they are not equal, return false.
5
If they are equal, call the function recursively on the substring excluding the first and last characters.
6
Return the result of the recursive call.
💻

Code

javascript
function isPalindrome(str) {
  if (str.length <= 1) return true;
  if (str[0] !== str[str.length - 1]) return false;
  return isPalindrome(str.slice(1, -1));
}

console.log(isPalindrome("racecar")); // true
console.log(isPalindrome("hello"));   // false
console.log(isPalindrome("a"));       // true
Output
true false true
🔍

Dry Run

Let's trace the string "racecar" through the code

1

Initial call

str = "racecar", first = 'r', last = 'r', they match, call isPalindrome("aceca")

2

Second call

str = "aceca", first = 'a', last = 'a', they match, call isPalindrome("cec")

3

Third call

str = "cec", first = 'c', last = 'c', they match, call isPalindrome("e")

4

Base case

str = "e", length is 1, return true

5

Return back

All previous calls return true, final result is true

CallStringFirst CharLast CharResult
1racecarrrrecurse with aceca
2acecaaarecurse with cec
3cecccrecurse with e
4eeetrue (base case)
💡

Why This Works

Step 1: Check base case

If the string length is 0 or 1, it means we have checked all pairs or the string is empty, so it is a palindrome.

Step 2: Compare characters

Compare the first and last characters of the string. If they differ, the string is not a palindrome.

Step 3: Recursive call

If the first and last characters match, call the function again on the substring that excludes these two characters to check the rest.

🔄

Alternative Approaches

Iterative approach
javascript
function isPalindromeIterative(str) {
  for (let i = 0; i < str.length / 2; i++) {
    if (str[i] !== str[str.length - 1 - i]) return false;
  }
  return true;
}

console.log(isPalindromeIterative("racecar")); // true
This uses a loop instead of recursion and is often more efficient in JavaScript because it avoids function call overhead.
Using built-in reverse
javascript
function isPalindromeReverse(str) {
  return str === str.split("").reverse().join("");
}

console.log(isPalindromeReverse("racecar")); // true
This is the simplest method but uses extra memory to create reversed string.

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

Time Complexity

The function compares pairs of characters from the outside moving inward, making about n/2 comparisons, so time is O(n).

Space Complexity

Each recursive call adds a new stack frame, and slicing the string creates new substrings, so space is O(n).

Which Approach is Fastest?

The iterative approach is fastest and uses less memory because it avoids recursion and substring creation.

ApproachTimeSpaceBest For
RecursiveO(n)O(n)Learning recursion and simple code
IterativeO(n)O(1)Performance and memory efficiency
Built-in reverseO(n)O(n)Quick and simple for small strings
💡
Always handle the base case first in recursion to avoid infinite calls.
⚠️
Forgetting to reduce the string size in the recursive call causes infinite recursion.