String Palindrome Check in JavaScript

Check a string palindrome in JavaScript: lowercase, strip spaces and symbols, then compare. "A man a plan a canal Panama" passes. Explanation and output.

BeginnerJavaScript String ProgramsExample 3 of 5
string-palindrome-check.js
1// Method 1: Compare with reversed string
2function isPalindrome1(str) {
3 str = str.toLowerCase().replace(/[^a-z0-9]/g, '');
4 return str === str.split('').reverse().join('');
5}
6
7console.log("madam:", isPalindrome1("madam"));
8console.log("hello:", isPalindrome1("hello"));
9console.log("A man a plan:", isPalindrome1("A man a plan a canal Panama"));
10
11// Method 2: Two-pointer approach
12function isPalindrome2(str) {
13 str = str.toLowerCase().replace(/[^a-z0-9]/g, '');
14 let left = 0;
15 let right = str.length - 1;
16
17 while (left < right) {
18 if (str[left] !== str[right]) {
19 return false;
20 }
21 left++;
22 right--;
23 }
24 return true;
25}
26
27console.log("\nTwo-pointer:");
28console.log("racecar:", isPalindrome2("racecar"));
29console.log("world:", isPalindrome2("world"));
30
31// Method 3: Recursive approach
32function isPalindrome3(str) {
33 str = str.toLowerCase().replace(/[^a-z0-9]/g, '');
34
35 if (str.length <= 1) return true;
36 if (str[0] !== str[str.length - 1]) return false;
37
38 return isPalindrome3(str.slice(1, -1));
39}
40
41console.log("\nRecursive:");
42console.log("level:", isPalindrome3("level"));
43console.log("hello:", isPalindrome3("hello"));
44
45// Method 4: Using every method
46function isPalindrome4(str) {
47 str = str.toLowerCase().replace(/[^a-z0-9]/g, '');
48 return str.split('').every((char, i) => {
49 return char === str[str.length - 1 - i];
50 });
51}
52
53console.log("\nUsing every:");
54console.log("deed:", isPalindrome4("deed"));

Sample Output

madam: true
hello: false
A man a plan: true

Two-pointer:
racecar: true
world: false

Recursive:
level: true
hello: false

Using every:
deed: true

In this one

  • How to clean text before comparing
  • Four ways to test a palindrome
  • How slice(1, -1) removes the ends
  • How every() checks all items

How it works

  1. 1

    "madam" and the Panama sentence are cleaned and match their reverse.

  2. 2

    Two pointers accept racecar and reject world.

  3. 3

    Recursion accepts level and rejects hello.

  4. 4

    every() accepts deed.

Edge cases

Punctuation

"Was it a car or a cat I saw?" fails without cleaning.

The regex removes the spaces and question mark first.

Accented letters

"Été" loses its é characters after cleaning.

The pattern only keeps a to z and 0 to 9. Extend it for other alphabets.

What's going on

A string palindrome reads the same forwards and backwards, like "madam" or "level". This page focuses on text, including full sentences.

Real sentences have capitals, spaces and punctuation. "A man a plan a canal Panama" is a palindrome only if you ignore those. So every method starts the same way. toLowerCase() makes all letters small. replace(/[^a-z0-9]/g, '') removes anything that is not a letter or digit. That pattern is a regular expression. [^...] means "not these characters" and g means "everywhere". Skip this cleaning and the spaces alone make the check fail.

Method 1 compares the cleaned string with its reverse. If they are equal, it is a palindrome.

Method 2 uses two pointers. left starts at index 0 and right at the last index. If the characters match, both move one step inward. The first mismatch returns false. This stops early and makes no copy.

Method 3 is recursive. It compares the first and last characters, then checks the middle with slice(1, -1). slice(1, -1) means "from index 1 up to, but not including, the last character". A string of length 0 or 1 is a palindrome.

Method 4 uses every(), which returns true only if the test passes for all items. Each character is compared with its mirror, str[str.length - 1 - i].

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

What does "Level".toLowerCase() return?

Pick an answer to check it.

Question 2

What does "abcd".slice(1, -1) return?

Pick an answer to check it.

Questions people ask

Why compare cleaned text?

People ignore spaces and capitals when reading a palindrome, so the code must too.

Is every() slower?

It checks every character twice in effect. Two pointers do half the work.

Does an empty string count?

All methods return true. Decide if your app should accept it.