Palindrome Check in JavaScript
Check if a number or word is a palindrome in JavaScript using string reverse, digit math, two pointers and recursion, with explanation and output.
1// Palindrome: reads same forwards and backwards2// Examples: 121, "madam", "racecar"34// Method 1: String palindrome5function isPalindromeString(str) {6 str = str.toLowerCase().replace(/[^a-z0-9]/g, '');7 let reversed = str.split('').reverse().join('');8 return str === reversed;9}1011console.log("madam:", isPalindromeString("madam"));12console.log("hello:", isPalindromeString("hello"));13console.log("A man a plan a canal Panama:", isPalindromeString("A man a plan a canal Panama"));1415// Method 2: Number palindrome16function isPalindromeNumber(num) {17 let original = num;18 let reversed = 0;1920 while (num > 0) {21 reversed = reversed * 10 + (num % 10);22 num = Math.floor(num / 10);23 }2425 return original === reversed;26}2728console.log("\nNumber palindrome:");29console.log("121:", isPalindromeNumber(121));30console.log("123:", isPalindromeNumber(123));31console.log("1221:", isPalindromeNumber(1221));3233// Method 3: Two-pointer approach (efficient)34function isPalindromeTwoPointer(str) {35 str = str.toLowerCase().replace(/[^a-z0-9]/g, '');36 let left = 0;37 let right = str.length - 1;3839 while (left < right) {40 if (str[left] !== str[right]) {41 return false;42 }43 left++;44 right--;45 }46 return true;47}4849console.log("\nTwo-pointer approach:");50console.log("racecar:", isPalindromeTwoPointer("racecar"));51console.log("hello:", isPalindromeTwoPointer("hello"));5253// Method 4: Recursive approach54function isPalindromeRecursive(str) {55 str = str.toLowerCase().replace(/[^a-z0-9]/g, '');5657 if (str.length <= 1) return true;58 if (str[0] !== str[str.length - 1]) return false;5960 return isPalindromeRecursive(str.slice(1, -1));61}6263console.log("\nRecursive approach:");64console.log("level:", isPalindromeRecursive("level"));65console.log("world:", isPalindromeRecursive("world"));
Sample Output
madam: true hello: false A man a plan a canal Panama: true Number palindrome: 121: true 123: false 1221: true Two-pointer approach: racecar: true hello: false Recursive approach: level: true world: false
In this one
- How to clean text with toLowerCase and a regex
- How to compare a value with its reverse
- How the two-pointer method saves work
- How a recursive check shrinks the problem
How it works
- 1
"madam" is cleaned, reversed and matches, so true.
- 2
"hello" reversed is "olleh", so false.
- 3
121 is reversed with digit math to 121, so true.
- 4
Two pointers check racecar from both ends.
- 5
The recursive check confirms level and rejects world.
Edge cases
Mixed case
Without toLowerCase, "Racecar" fails.
R and r are different characters. Lowercase first.
Negative numbers
isPalindromeNumber(-121) returns false.
The minus sign only appears at the front. This program treats negatives as not palindromes.
Empty string
isPalindromeString("") returns true.
An empty string equals its reverse. Decide if that should count in your app.
What's going on
A palindrome reads the same forwards and backwards. 121 is one. 10 is not, because backwards it is 01. Words like madam, level and racecar are palindromes too.
Method 1 handles strings. First it cleans the text. toLowerCase() makes Racecar and racecar the same. replace(/[^a-z0-9]/g, '') removes everything that is not a letter or digit, such as spaces and commas. A regular expression, or regex, is a pattern for matching text. After cleaning, "A man a plan a canal Panama" becomes "amanaplanacanalpanama". Then the code reverses the string and compares with ===. Skip the cleaning and spaces or capitals make it fail.
Method 2 handles numbers with the same digit loop as the reverse-number program. It keeps the original value, builds the reversed number, and compares the two. Negative numbers return false here, because the loop never runs and -121 is not equal to 0. This file treats negatives as not palindromes.
Method 3 uses two pointers. left starts at the first character and right at the last. If they match, both move inward. If any pair differs, the answer is false right away. This avoids building a reversed copy.
Method 4 uses recursion. It compares the first and last characters, then calls itself on the middle part with slice(1, -1). A string of length 0 or 1 is always a palindrome.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Is 1221 a palindrome?
Pick an answer to check it.
Question 2
Why lowercase the string first?
Pick an answer to check it.
Questions people ask
What does /[^a-z0-9]/g mean?
Match any character that is not a lowercase letter or digit, everywhere in the string. replace removes them.
Which method is fastest?
Two pointers. It stops at the first mismatch and makes no copy.
Is a single digit a palindrome?
Yes. 7 reads the same both ways.