Strings · beginner
JavaScript Program to Check if a String is a Palindrome
Check whether a string is a palindrome in JavaScript, ignoring case, spaces and punctuation with a regular expression, plus a two-pointer version.
- toLowerCase()
- replace() with a regex
- split / reverse / join
- two pointers
A palindrome reads the same forwards and backwards: "level", "racecar" or 12321. Sentences such as "A man, a plan, a canal: Panama" count too once case, spaces and punctuation are ignored, so the program cleans the text first.
JavaScript strings have no reverse() method, so the usual trick is to split the string into an array of characters, reverse the array and join it again.
The program
function isPalindrome(text) {
const cleaned = text.toLowerCase().replace(/[^a-z0-9]/g, "");
const reversed = cleaned.split("").reverse().join("");
return cleaned === reversed;
}
for (const text of ["racecar", "JavaScript", "A man, a plan, a canal: Panama", "12321"]) {
console.log(JSON.stringify(text), isPalindrome(text));
}"racecar" true
"JavaScript" false
"A man, a plan, a canal: Panama" true
"12321" trueHow it works
- toLowerCase() makes the check case-insensitive.
- replace(/[^a-z0-9]/g, "") removes every character that is not a letter or digit; the g flag replaces all of them, not just the first.
- split("") makes an array of characters, reverse() reverses it in place, and join("") turns it back into a string.
- If the cleaned text equals its reverse, it is a palindrome.
Watch it run line by line
In the isPalindrome frame for the Panama sentence, cleaned is "amanaplanacanalpanama" and reversed is the same string, so the return row shows true.
“Visualize step by step” opens the program in the JavaScript Compiler and records every step in your browser. Use the arrow keys to move forwards and backwards; changed variables are highlighted.
Another way: Two pointers, no reversed copy
Compare characters from both ends and move inwards. It stops at the first mismatch and needs no extra string, which is what interviewers usually ask for next.
function isPalindrome(s) {
let left = 0;
let right = s.length - 1;
while (left < right) {
if (s[left] !== s[right]) return false;
left++;
right--;
}
return true;
}
console.log(isPalindrome("level"), isPalindrome("levels"), isPalindrome(String(12321)));true false trueCommon mistakes
- Calling text.reverse(): strings have no reverse method, so this throws TypeError.
- Forgetting the g flag: replace(/[^a-z0-9]/, "") removes only the first unwanted character.
- Cleaning with /[^a-z]/ only, which drops digits, so "12321" becomes an empty string (and looks like a palindrome).
- split("") splits emoji and some accented letters into halves; use Array.from(text) for those.
Try it yourself
- Find the palindromic numbers between 100 and 200.
- Find the longest palindromic word in a sentence.
Open the program in the JavaScript Compiler, change it and press Run: there is nothing to install, and your code never leaves your browser.
Related examples
Browse all 25 JavaScript examples, or write your own program in the online JavaScript Compiler.