
The Palindrome Number Problem: Strings vs. Maths in JavaScript

In the world of algorithms, palindromes frequently pop up as intriguing structures to investigate. A palindrome is something that reads the same backwards as it does forwards, and while they are often associated with words like radar or racecar, they can apply to numbers too (like 121).
If you want to read the original problem first, you can find Palindrome Number on LeetCode. But how do we determine if a number is a palindrome? The answer lies in two main approaches: string manipulation and mathematical operations.
Understanding the Problem
Given an integer x, determine whether it is a palindrome. An integer is a palindrome when it reads the same backwards as forwards. For instance, 121 is a palindrome, whilst ‑121 is not (it reads 121‑ backwards).
The catch here is that some solutions, like converting the integer to a string to check for a palindrome, might seem straightforward, but may not be particularly optimal.
Constraints
- Do not convert the integer into a string to solve the problem (although, for the sake of exploration, I will consider this method too).
- Consider negative numbers as non‑palindromic.
‑121is not the same as121‑.
This problem serves as an excellent exercise in understanding number manipulation, string operations, and the nuances between different algorithmic solutions. As we delve deeper, I'll describe both string‑based and mathematical methods, evaluating the pros and cons of each.
Setting the Scene
When we see a number (like 1234321), it is fairly evident to our human eyes that it reads the same backwards. However, for a computer, it's not as straightforward.
There are two main approaches that can be considered in solving this:
- Convert the number to a string and check for equality.
- Use mathematical operations to reverse the number and compare.
The String Method
Personally, this is the method I reached for first. It is quite intuitive to convert the number to a string and then check if it is equal when read forwards and backwards.
For example:
const isPalindromeString = (num: number): boolean => { const str = num.toString(); return str === str.split('').reverse().join('');};This method is simple, but comes with the overhead of string operations, and is unlikely to be efficient for very large numbers.
The Mathematical Method
Instead of relying on string manipulations, we can reverse the number mathematically and then check if the original and reversed numbers are the same. Like this:
const isPalindromeMath = (num: number): boolean => { if (num < 0 || (num % 10 === 0 && num !== 0)) return false; let reversed = 0; while (num > reversed) { reversed = reversed * 10 + (num % 10); num = Math.floor(num / 10); } return num === reversed || num === Math.floor(reversed / 10);};If you have read other articles on my blog, you might recognise that this is very similar to another integer problem: reversing numbers ‑ coincidentally also with a string‑based and a maths‑based solution.
How It Works
- Negative numbers are not palindromic by definition in this context.
- We reverse half of the number to avoid overflow issues.
- If the length of the number is odd, the middle digit doesn't matter in palindromicity, so we floor‑divide the reversed number by 10.
Why Maths Over String?
Although the string‑based solution is straightforward, the mathematical solution offers several advantages:
Performance:
String operations, especially reverse, can be costly for very long strings. Directly manipulating numbers might be faster.Memory Efficiency:
The string method can be memory‑intensive due to the creation of new string instances.Versatility:
The mathematical solution works in environments that may not support string operations conveniently ‑ although this seems unlikely in JavaScript!
Final Thoughts
When it comes to solving the Palindrome Number Problem, both string‑based and mathematics‑based methods achieve the same goal. However, in programming ‑ as in life ‑ there's often more than one way to skin a cat. The string method might suffice for small‑scale applications or for quickly testing an idea. However, for optimised solutions, especially in environments where performance matters, the mathematical approach shines.
By understanding these underlying mechanisms, web developers can make informed decisions, optimising not just for solution correctness but also for efficiency and scalability.
Related Articles

Building Design Systems for Web Applications with Figma, Storybook, and npm. 
What Skills are Required for a Front‑End Developer? What Skills are Required for a Front‑End Developer?

Building a Custom Vue 3 Hook Using the Composition API. Building a Custom Vue 3 Hook Using the Composition API

Multi‑Source BFS: Solving the 'Rotting Oranges' Problem. Multi‑Source BFS: Solving the 'Rotting Oranges' Problem

Memoization in JavaScript: Optimising Function Calls. Memoization in JavaScript: Optimising Function Calls

Five Tips for Transitioning from Permanent to Freelancing. Five Tips for Transitioning from Permanent to Freelancing

Classes in JavaScript: An Introduction. Classes in JavaScript: An Introduction

3Sum Closest in JavaScript: Sorting and Two Pointers. 3Sum Closest in JavaScript: Sorting and Two Pointers

Understanding the Difference Between <b> and <strong>. Understanding the Difference Between
<b>and<strong>
Optimising Next.js Performance with Incremental Static Regeneration (ISR). Optimising Next.js Performance with Incremental Static Regeneration (ISR)

CSS box‑sizing: Controlling the Element Box Model. CSS
box‑sizing: Controlling the Element Box Model
Controlled vs. Uncontrolled Components in React. Controlled vs. Uncontrolled Components in React