Palindrome Check

Program to check if a number or string is palindrome

BeginnerTopic: Loop Programs
Back

JavaScript Palindrome Check Program

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

Try This Code
// Palindrome: reads same forwards and backwards
// Examples: 121, "madam", "racecar"

// Method 1: String palindrome
function isPalindromeString(str) {
    str = str.toLowerCase().replace(/[^a-z0-9]/g, '');
    let reversed = str.split('').reverse().join('');
    return str === reversed;
}

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

// Method 2: Number palindrome
function isPalindromeNumber(num) {
    let original = num;
    let reversed = 0;
    
    while (num > 0) {
        reversed = reversed * 10 + (num % 10);
        num = Math.floor(num / 10);
    }
    
    return original === reversed;
}

console.log("\nNumber palindrome:");
console.log("121:", isPalindromeNumber(121));
console.log("123:", isPalindromeNumber(123));
console.log("1221:", isPalindromeNumber(1221));

// Method 3: Two-pointer approach (efficient)
function isPalindromeTwoPointer(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 approach:");
console.log("racecar:", isPalindromeTwoPointer("racecar"));
console.log("hello:", isPalindromeTwoPointer("hello"));

// Method 4: Recursive approach
function isPalindromeRecursive(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 isPalindromeRecursive(str.slice(1, -1));
}

console.log("\nRecursive approach:");
console.log("level:", isPalindromeRecursive("level"));
console.log("world:", isPalindromeRecursive("world"));
Output
madam: true
hello: false
A man a plan a canal Panama: true

Number palindrome:
121: true
123: false
1221: true

Two-pointer approach:
racecar: true
hello: false

Recursive approach:
level: true
world: false

Understanding Palindrome Check

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

Palindrome Definition

A palindrome reads the same forwards and backwards:

Numbers: 121, 1221, 1331
Strings: "madam", "racecar", "level"
Phrases: "A man a plan a canal Panama"

Method 1: String Reversal

Compare original with reversed:

let reversed = str.split('').reverse().join('');
return str === reversed;

String Cleaning:

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

Method 2: Number Palindrome

Reverse number and compare:

let reversed = 0;
while (num > 0) {
    reversed = reversed * 10 + (num % 10);
    num = Math.floor(num / 10);
}
return original === reversed;

Method 3: Two-Pointer Approach

Compare characters from both ends:

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 4: Recursive

Check first and last, recurse on middle:

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

String Methods:

slice(1, -1): Remove first and last character
charAt(0): Get first character
length: String length

Time Complexity:

Reversal: O(n)
Two-pointer: O(n) - Best!
Recursive: O(n)

When to Use:

-

Reversal

: Simple, readable

-

Two-pointer

: Most efficient

-

Recursive

: Learning recursion

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 Palindrome Check

This JavaScript program is part of the "Loop 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