Check Palindrome Number in JavaScript

Beginner
⏱️ 8 min read
📚 Updated: May 2026
🎯 2 Code Examples
Digit reversal

What you’ll learn

  • What a palindrome number is (same digits forward and backward).
  • How to reverse digits with % 10 and / 10, then compare to the original.
  • A second program that prints every palindrome between 100 and 200, plus a live preview.

Overview

Think of 121: front to back is “1-2-1,” and back to front is the same. The program peels off the last digit repeatedly, builds the reversed number, and checks equality.

Single check

Try 121 or 12321.

Range scan

Every palindrome from 100 to 200.

Live preview

Nonnegative integers only (matches the JavaScript examples here).

Prerequisites

while loops, integer division, and % for the last digit.

  • Basic JavaScript syntax, functions, and console.log.
  • Comfort reading multi-digit integers digit-by-digit.

The idea

Take 121. Chop the last digit (1), remember it. Chop again (2), shift what you remembered left and add. Chop again (1). You rebuilt 121—if that equals what you started with, it is a palindrome.

Same trick works for 9009, 5, or 0.

Live preview

Enter a nonnegative integer. Reverses digits the same way as Example 1.

Try 121, 123, 0, or 7. Negative input is rejected here.

Live result
Press “Check palindrome.”

Algorithm

Goal: decide whether the decimal digits of n read the same forward and backward.

Remember original

You will change a working copy of n; keep the first value safe.

Reverse digits

Repeatedly: digit = n % 10, rev = rev * 10 + digit, n = floor(n / 10) until n === 0.

Compare

If rev === original, you have a palindrome.

📜 Pseudocode

Pseudocode
function isPalindrome(n):
    original = n
    reversed = 0
    while n != 0:
        reversed = reversed * 10 + (n mod 10)
        n = floor(n / 10)
    return original == reversed
1

Check one number

Classic interview structure: 121 reads 121 backward too.

JavaScript
function isPalindrome(number) {
  const originalNumber = number;
  let reversedNumber = 0;

  while (number !== 0) {
    const remainder = number % 10;
    reversedNumber = reversedNumber * 10 + remainder;
    number = Math.floor(number / 10);
  }

  return originalNumber === reversedNumber;
}

const number = 121;
if (isPalindrome(number)) {
  console.log(number + " is a palindrome number.");
} else {
  console.log(number + " is not a palindrome number.");
}
Try it Yourself

Explanation

The loop strips digits from number until it becomes 0. reversedNumber accumulates the flipped version; comparing with originalNumber finishes the test.

2

Palindromes from 100 to 200

Three-digit palindromes in this band look like aba: hundreds digit equals units digit. The loop prints each hit.

JavaScript
function isPalindrome(num) {
  const originalNum = num;
  let reversedNum = 0;

  while (num !== 0) {
    reversedNum = reversedNum * 10 + (num % 10);
    num = Math.floor(num / 10);
  }

  return originalNum === reversedNum;
}

console.log("Palindrome numbers in the range 100 to 200:");

const values = [];
for (let i = 100; i <= 200; i++) {
  if (isPalindrome(i)) {
    values.push(String(i));
  }
}
console.log(values.join(" "));
Try it Yourself

Explanation

Same helper as Example 1; the outer for walks every candidate in the inclusive range.

Notes

Overflow. For very large inputs, reversedNumber * 10 can exceed JavaScript integer precision; string-based checks can avoid arithmetic limits.

Alternatives. Converting to a string and comparing indices from both ends avoids arithmetic overflow but uses extra memory.

❓ FAQ

Write the digits on paper. Read them left-to-right and right-to-left. If both readings are the same (like 121 or 9009), the number is a palindrome.
Rebuilding the number backward gives you something easy to compare with the original using ===. That is the classic loop interview solution.
Yes. Digits like 3 or 8 read the same forward and backward.
Yes. It has one digit and reads the same either way.
These snippets assume nonnegative integers. This keeps the digit-reversal logic beginner-friendly and avoids sign-handling confusion.
Reversing an n-digit number costs O(number of digits), roughly O(log10 value) for positive integers. Scanning a range multiplies by how many integers you test.

🔄 Input / output examples

Swap number in Example 1 or change the loop bounds in Example 2.

ValuePalindrome?
0Yes
7Yes
121Yes
123No (321)

Edge cases

Trailing zeros

120 vs 21

Integer reversal drops trailing zeros on the right (they become leading zeros, which disappear). So 120 reverses to 21—not a palindrome unless you treat numbers with leading-zero strings.

Single digit

n < 10

Always palindromes for nonnegative n.

⏱️ Time and space complexity

OperationTimeExtra space
isPalindrome(n)O(d) digitsO(1)
Range [a, b]O((b-a+1) * d) worst caseO(1)

Summary

  • Core trick: reverse digits with % 10 and Math.floor(/ 10), compare to the saved original.
  • Ranges: reuse isPalindrome inside a for loop.
  • Watch-outs: precision limits on huge values; negatives need a separate plan.
Did you know?

The word palindrome also describes words like “radar” or “level”—for integers we only compare digits, so single-digit numbers like 7 always pass.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful