0
0
JavascriptProgramBeginner · 2 min read

JavaScript Program to Check Palindrome String

You can check if a string is a palindrome in JavaScript by comparing it to its reversed version using str === str.split('').reverse().join('').
📋

Examples

Inputmadam
Outputtrue
Inputhello
Outputfalse
Inputracecar
Outputtrue
🧠

How to Think About It

To check if a string is a palindrome, think about reading it forwards and backwards. If both are exactly the same, the string is a palindrome. So, reverse the string and compare it to the original.
📐

Algorithm

1
Get the input string.
2
Reverse the string by splitting it into characters, reversing the array, and joining it back.
3
Compare the original string with the reversed string.
4
If both are equal, return true; otherwise, return false.
💻

Code

javascript
function isPalindrome(str) {
  return str === str.split('').reverse().join('');
}

console.log(isPalindrome('madam'));
console.log(isPalindrome('hello'));
console.log(isPalindrome('racecar'));
Output
true false true
🔍

Dry Run

Let's trace the string 'madam' through the code

1

Input string

str = 'madam'

2

Split string into array

['m', 'a', 'd', 'a', 'm']

3

Reverse array

['m', 'a', 'd', 'a', 'm']

4

Join array back to string

'madam'

5

Compare original and reversed

'madam' === 'madam' => true

Original StringReversed StringIs Palindrome?
madammadamtrue
💡

Why This Works

Step 1: Splitting the string

We use split('') to turn the string into an array of characters so we can reverse it.

Step 2: Reversing the array

The reverse() method flips the order of characters in the array.

Step 3: Joining back to string

We use join('') to combine the reversed array back into a string for comparison.

Step 4: Comparing strings

If the original string and reversed string are exactly the same, the string is a palindrome.

🔄

Alternative Approaches

Two-pointer comparison
javascript
function isPalindrome(str) {
  let left = 0;
  let right = str.length - 1;
  while (left < right) {
    if (str[left] !== str[right]) return false;
    left++;
    right--;
  }
  return true;
}

console.log(isPalindrome('madam'));
console.log(isPalindrome('hello'));
This method checks characters from both ends without extra memory for reversed string, better for large strings.
Using a for loop to build reversed string
javascript
function isPalindrome(str) {
  let reversed = '';
  for (let i = str.length - 1; i >= 0; i--) {
    reversed += str[i];
  }
  return str === reversed;
}

console.log(isPalindrome('racecar'));
console.log(isPalindrome('test'));
This builds the reversed string manually, which is easy to understand but less efficient than built-in methods.

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

Time Complexity

The program processes each character once to reverse the string, so it runs in linear time relative to string length.

Space Complexity

It uses extra space to store the reversed string, which is proportional to the input size.

Which Approach is Fastest?

The two-pointer method uses O(1) space and is faster for large strings, while the built-in reverse method is simpler but uses extra memory.

ApproachTimeSpaceBest For
Built-in reverseO(n)O(n)Simple and short code
Two-pointer comparisonO(n)O(1)Large strings, memory efficient
Manual reverse with loopO(n)O(n)Clear step-by-step logic
💡
Use built-in string and array methods like split, reverse, and join for concise palindrome checks.
⚠️
Forgetting to compare the original string with the reversed string exactly, or mixing up the order of operations.