String Palindrome Check

Program to check if a string is palindrome

BeginnerTopic: String Programs
Back

JavaScript String Palindrome Check Program

This program helps you to learn the fundamental structure and syntax of JavaScript programming.

Try This Code
// Method 1: Compare with reversed string
function isPalindrome1(str) {
    str = str.toLowerCase().replace(/[^a-z0-9]/g, '');
    return str === str.split('').reverse().join('');
}

console.log("madam:", isPalindrome1("madam"));
console.log("hello:", isPalindrome1("hello"));
console.log("A man a plan:", isPalindrome1("A man a plan a canal Panama"));

// Method 2: Two-pointer approach
function isPalindrome2(str) {
    str = str.toLowerCase().replace(/[^a-z0-9]/g, '');
    let left = 0;
    let right = str.length - 1;
    
    while (left < right) {
        if (str[left] !== str[right]) {
            return false;
        }
        left++;
        right--;
    }
    return true;
}

console.log("\nTwo-pointer:");
console.log("racecar:", isPalindrome2("racecar"));
console.log("world:", isPalindrome2("world"));

// Method 3: Recursive approach
function isPalindrome3(str) {
    str = str.toLowerCase().replace(/[^a-z0-9]/g, '');
    
    if (str.length <= 1) return true;
    if (str[0] !== str[str.length - 1]) return false;
    
    return isPalindrome3(str.slice(1, -1));
}

console.log("\nRecursive:");
console.log("level:", isPalindrome3("level"));
console.log("hello:", isPalindrome3("hello"));

// Method 4: Using every method
function isPalindrome4(str) {
    str = str.toLowerCase().replace(/[^a-z0-9]/g, '');
    return str.split('').every((char, i) => {
        return char === str[str.length - 1 - i];
    });
}

console.log("\nUsing every:");
console.log("deed:", isPalindrome4("deed"));
Output
madam: true
hello: false
A man a plan: true

Two-pointer:
racecar: true
world: false

Recursive:
level: true
hello: false

Using every:
deed: true

Understanding String Palindrome Check

This program demonstrates different methods to check if a string is a palindrome.

Palindrome Definition

Reads same forwards and backwards:

"madam" → "madam" ✅
"hello" → "olleh" ❌

Method 1: Compare with Reversed

Simple approach:

str === str.split('').reverse().join('');

String Cleaning:

toLowerCase(): Case-insensitive
replace(/[^a-z0-9]/g, ''): Remove non-alphanumeric

Method 2: Two-Pointer

Most efficient:

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

Pros:

O(n) time, O(1) space
No string reversal needed
Most efficient

Method 3: Recursive

Elegant but less efficient:

if (str[0] !== str[str.length - 1]) return false;
return isPalindrome(str.slice(1, -1));

Method 4: Every Method

Functional approach:

str.split('').every((char, i) => {
});
    return char === str[str.length - 1 - i];

Every Method:

Tests if all elements pass test
Returns false on first failure

Time Complexity:

All methods: O(n)
Two-pointer: Best space efficiency

When to Use:

-

Reversed comparison

: Simplest

-

Two-pointer

: Most efficient

-

Recursive

: Learning recursion

-

Every

: Functional style

Let us now understand every line and the components of the above program.

Note: To write and run JavaScript programs, you need to set up the local environment on your computer. Refer to the complete article Setting up JavaScript Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your JavaScript programs.

Practical Learning Notes for String Palindrome Check

This JavaScript program is part of the "String Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.

A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.

For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.

Table of Contents