Verification if a number is Palindrome in JavaScript

A palindrome number reads the same forwards and backwards. In JavaScript, we can check if a number is palindrome without converting it to a string by mathematically extracting and comparing digits.

Palindrome numbers are those numbers which read the same from both backward and forward.

For example:

121
343
12321

Algorithm Approach

The algorithm works by:

  • Finding a factor to extract the first digit
  • Comparing first and last digits
  • Removing both digits and repeating until all digits are checked

Example

const isPalindrome = (num) => {
    // Finding the appropriate factor to extract the first digit
    let factor = 1;
    while (num / factor >= 10){
        factor *= 10;
    }
    while (num) {
        let first = Math.floor(num / factor);
        let last = num % 10;
        // If first and last digit not same return false
        if (first != last){
            return false;
        }
        // Removing the first and last digit from number
        num = Math.floor((num % factor) / 10);
        // Reducing factor by a factor of 2 as 2 digits are dropped
        factor = factor / 100;
    }
    return true;
};

console.log(isPalindrome(123241));
console.log(isPalindrome(12321));
console.log(isPalindrome(145232541));
console.log(isPalindrome(1231));

Output

false
true
true
false

Alternative Method: Number Reversal

Another approach is to reverse the number and compare it with the original:

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

console.log(isPalindromeReverse(121));
console.log(isPalindromeReverse(123));
console.log(isPalindromeReverse(12321));

Output

true
false
true

How It Works

The first method extracts digits from both ends and compares them. The factor helps locate the first digit by dividing the number appropriately. After each comparison, both first and last digits are removed.

The second method reverses the entire number mathematically and compares it with the original, which is simpler but requires storing the original value.

Conclusion

Both methods effectively check palindrome numbers without string conversion. The digit comparison approach is more memory efficient, while the reversal method is easier to understand and implement.

Updated on: 2026-03-15T23:18:59+05:30

696 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements