0
0
JavascriptProgramBeginner · 2 min read

JavaScript Program to Check Palindrome Number

You can check if a number is a palindrome in JavaScript by converting it to a string and comparing it with its reverse using num.toString() === num.toString().split('').reverse().join('').
📋

Examples

Input121
Outputtrue
Input123
Outputfalse
Input0
Outputtrue
🧠

How to Think About It

To check if a number is a palindrome, think of it as a word that reads the same forwards and backwards. Convert the number to a string, reverse that string, and then compare it to the original string. If both are the same, the number is a palindrome.
📐

Algorithm

1
Get the input number.
2
Convert the number to a string.
3
Reverse the string.
4
Compare the reversed string with the original string.
5
If they are equal, return true; otherwise, return false.
💻

Code

javascript
function isPalindromeNumber(num) {
  const str = num.toString();
  const reversed = str.split('').reverse().join('');
  return str === reversed;
}

console.log(isPalindromeNumber(121)); // true
console.log(isPalindromeNumber(123)); // false
console.log(isPalindromeNumber(0));   // true
Output
true false true
🔍

Dry Run

Let's trace the number 121 through the code to see how it checks for palindrome.

1

Convert number to string

num = 121, str = '121'

2

Reverse the string

str = '121', reversed = '121'

3

Compare original and reversed

'121' === '121' is true

4

Return result

Function returns true

numstrreversedisPalindrome
121'121''121'true
💡

Why This Works

Step 1: Convert number to string

We use toString() to treat the number like text so we can reverse it easily.

Step 2: Reverse the string

Splitting the string into characters, reversing the array, and joining back creates the reversed version.

Step 3: Compare original and reversed

If the reversed string matches the original, the number reads the same forwards and backwards.

🔄

Alternative Approaches

Mathematical reversal
javascript
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(isPalindromeNumber(121)); // true
console.log(isPalindromeNumber(123)); // false
This method avoids string conversion and works purely with numbers, which can be faster for large numbers.
Recursive string check
javascript
function isPalindromeNumber(num) {
  const str = num.toString();
  function check(s, start, end) {
    if (start >= end) return true;
    if (s[start] !== s[end]) return false;
    return check(s, start + 1, end - 1);
  }
  return check(str, 0, str.length - 1);
}

console.log(isPalindromeNumber(121)); // true
console.log(isPalindromeNumber(123)); // false
This uses recursion to compare characters from both ends, which is elegant but uses more memory.

Complexity: O(n) time, O(n) space

Time Complexity

The time depends on the number of digits n because we convert to string and reverse it, which takes O(n) time.

Space Complexity

Extra space is used to store the string and its reversed version, so space is O(n).

Which Approach is Fastest?

The mathematical reversal avoids string operations and uses O(1) space, making it faster and more memory efficient for large numbers.

ApproachTimeSpaceBest For
String reversalO(n)O(n)Simplicity and readability
Mathematical reversalO(n)O(1)Performance and memory efficiency
Recursive string checkO(n)O(n)Learning recursion and elegant code
💡
Convert the number to a string to easily reverse and compare it for palindrome checking.
⚠️
Forgetting to convert the number to a string before reversing causes errors or wrong results.