Check Palindrome Number in JavaScript
What you’ll learn
- What a palindrome number is (same digits forward and backward).
- How to reverse digits with
% 10and/ 10, then compare to the original. - A second program that prints every palindrome between
100and200, 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.
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
function isPalindrome(n):
original = n
reversed = 0
while n != 0:
reversed = reversed * 10 + (n mod 10)
n = floor(n / 10)
return original == reversedCheck one number
Classic interview structure: 121 reads 121 backward too.
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.");
}Explanation
The loop strips digits from number until it becomes 0. reversedNumber accumulates the flipped version; comparing with originalNumber finishes the test.
Palindromes from 100 to 200
Three-digit palindromes in this band look like aba: hundreds digit equals units digit. The loop prints each hit.
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(" "));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
🔄 Input / output examples
Swap number in Example 1 or change the loop bounds in Example 2.
| Value | Palindrome? |
|---|---|
0 | Yes |
7 | Yes |
121 | Yes |
123 | No (321) |
Edge cases
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.
n < 10
Always palindromes for nonnegative n.
⏱️ Time and space complexity
| Operation | Time | Extra space |
|---|---|---|
isPalindrome(n) | O(d) digits | O(1) |
Range [a, b] | O((b-a+1) * d) worst case | O(1) |
Summary
- Core trick: reverse digits with
% 10andMath.floor(/ 10), compare to the saved original. - Ranges: reuse
isPalindromeinside aforloop. - Watch-outs: precision limits on huge values; negatives need a separate plan.
The word palindrome also describes words like “radar” or “level”—for integers we only compare digits, so single-digit numbers like 7 always pass.
8 people found this page helpful
