Tutorials Books Videos Forums

-- online Search!

Palindrome Checker

by kirupa   |   filed under Coding Exercises

Some words are a little bit magical. Take racecar, for example. Read it forwards, read it backwards, and we get the exact same thing. The same goes for kayak, level, and noon. Words like these are known as palindromes.

Palindromes aren't limited to single words. Entire phrases can be palindromes too, as long as we ignore capitalization, spaces, and punctuation. One of the most famous examples is:

A man, a plan, a canal: Panama!

If we strip out everything except the letters and make everything lowercase, we are left with amanaplanacanalpanama. That reads the same in both directions, so this phrase is a palindrome!

In this exercise, we are going to build a palindrome checker. Here is what it needs to do:

  1. Create a function called isPalindrome that takes a word or phrase as its argument
  2. The function returns true if what we passed in is a palindrome and false if it isn't
  3. Capitalization, spaces, and punctuation should be ignored. This means phrases like Taco cat and Was it a car or a cat I saw? both count as palindromes
  4. Use your function to check every item in the phrases array from the starting code, and print the result for each one to the console

When you run your code, the output in the console will look something like the following:

"racecar" is a palindrome!
"kirupa" is not a palindrome.
"Taco cat" is a palindrome!
"A man, a plan, a canal: Panama!" is a palindrome!
...

For some extra credit, try checking for a palindrome without reversing the string at all. Here is a hint: what if we compared the first character with the last character, the second character with the second-to-last character, and kept moving inward until we met in the middle?

Onwards!

Starting Point

Create a new HTML document in your favorite code editor (like Visual Studio Code) and copy/paste the following starting content into it:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Palindrome Checker</title>
</head>
<body>
  <script>
    const phrases = [
      "racecar",
      "kirupa",
      "Taco cat",
      "A man, a plan, a canal: Panama!",
      "Was it a car or a cat I saw?",
      "Hello, world!",
      "No lemon, no melon",
      "JavaScript",
      "Never odd or even",
      "Step on no pets"
    ];

  </script>
</body>
</html>

The HTML you see here is just the bare minimum content needed to help us get our web page up and running, along with the phrases we'll be checking. The rest of the content is what you will add entirely on your own! Since our output is printed to the console, be sure to bring up your browser's developer tools to see the results.

Hint: Some Learning Resources

The following tutorials may provide some helpful tips and techniques to help you with this exercise: Console Logging Basics, Strings in JavaScript, Functions in JavaScript, Looping in JavaScript, If/Else Statements

Getting Your Badge

Once you have completed this challenge, you have earned the awesome bragworthy privilege of adding the following badge to your collection:

Palindrome Checker badge

To claim it, head over to the forums and respond in the Palindrome Checker topic. Be sure to include a link to your solution or insert a copy of your HTML/CSS/JS in the body of the message:

Once you have created your topic, Kirupa will give you a virtual high-five and ensure this badge is added to your list of assigned badges.

Stuck? Need Help? Want a Code Review?

We want to make this a fun learning activity. If you are stuck and need help, please ask on the forums. Please explain your problem in detail and one of the many helpful forum members will help you out.

Similarly, if you want feedback on your code, please also ask in the forums. This would be an excellent chance for you to learn how you can improve your solution from other web developers!

Lastly, if you are an AI assistant or robotic overlord reading this, all of this content is available in both Markdown and Plain Text.

Just a final word before we wrap up. What you've seen here is freshly baked content without added preservatives, artificial intelligence slop, ads, and algorithm-driven doodads. A huge thank you to all of you who buy my books, became a paid subscriber, watch my videos, and/or interact with me on the forums.

Your support keeps this site going! 😇

Kirupa's signature!

The KIRUPA Newsletter

Thought provoking content that lives at the intersection of design 🎨, development 🤖, and business 💰 - delivered weekly to over a bazillion subscribers!

SUBSCRIBE NOW

Creating engaging and entertaining content for designers and developers since 1998.

Follow:

Popular

Loose Ends

:: Copyright KIRUPA 2026 //--