Easy ways to check for palindromes using JavaScript

Easy ways to check for palindromes using JavaScript

·

4 min read

What is a palindrome

A palindrome is a word, number or string that can be reversed and it'll be the same result. One good example would be the word Racecar or Taco Cat.

Now let's create some functions that let us check when words are palindromes or not.

For Loop

This is the most intuitive one; let's ask our user for input and pass it to our checkPalindrome function.

We will need to loop over the word and check if one half doesn't match with the other one, if that's the case, it's not a palindrome, else it is a palindrome.

const stringToCheck = prompt("Enter word to check");

function checkPalindrome(stringToCheck){
//we need to know the length
    const len = stringToCheck.length
//let's loop over the input
    for(let i=0; i < len / 2; i++){
//check if the characters of the first half are not the same as the other half
        if(stringToCheck[i] !== stringToCheck[len - 1 - i]){
//if they're not it's not a palindrome
            return ("It's not a palindrome")
        }
    }
//if they are it IS a palindrome
    return ("It's a palindrome")
}

checkPalindrome(stringToCheck);

Let's try our program out:

hello -> "It's not a palindrome"
1001 -> "It's a palindrome"
Hannah -> "It's a palindrome"
Taco cat -> "It's not a palindrome"

Uh oh!! This will ONLY work with words, we need to adjust our function to eliminate white-space so we can check if Taco cat is a palindrome, we must!!

We can do that changing our string variable and using RegEx while also setting all the input to lower case to ensure our program works even on things like "TaCo cAT".

const stringToCheck = prompt("Enter word to check");

function checkPalindrome(stringToCheck){
//Here we use a built in to replace every blank space and we set the whole
//string to toLowerCase.
    const newStr = stringToCheck.replace(/[\W_]/g, "").toLowerCase();
//the rest of the code stays the same only changing our stringToCheck
//for the newStr variable

    const len = newStr.length

    for(let i=0; i < len / 2; i++){
        if(newStr[i] !== newStr[len - 1 - i]){
            return ("It's not a palindrome")
        }
    }
    return ("It's a palindrome")
}

checkPalindrome(stringToCheck);

Now if we check for:

Taco cat -> "It's a palindrome"
Hannah Hannah -> "It's a palindrome" 
Hannah Madam -> "It's not a palindrome"
TaCo cAT -> "It's a palindrome"
11 11 -> "It's a palindrome"

JavaScript inbuilt methods

Instead of looping over the whole string, let's create a variable to store the reversed string and compare it to the input, if we get a match, it's a palindrome.

const string = prompt("Enter palindrome to check")

function checkForPalindrome(string){
    const newString = string.replace(/[\W_]/g, "").toLowerCase();

//we use the JS inbuilt methods to split it into an array
//reverse it and join it back together as a string
    const reversedString = newString.split("").reverse().join("");
//we use an if statement to check if it's not 
    if(newString !== reversedString){
        return("It's not a palindrome")
    } else {
//if it is
        return("It's a palindrome")
    }

}

checkForPalindrome(string)

Now let's check for:

Hello -> "It's not a palindrome"
Taco cat -> "It's a palindrome"
11 11 -> "It's a palindrome"

We can make it prettier and change that if statement with the ternary operator.

const string = prompt("Enter palindrome to check")

function checkForPalindrome(string){
    const newString = string.replace(/[\W_]/g, "").toLowerCase();
    const reversedString = newString.split("").reverse().join("");

return newString === reversedString ? "It's a palindrome" : "It's not a palindrome"    
}

checkForPalindrome(string)

We get the same results, yey!

Hello -> "It's not a palindrome"
Taco cat -> "It's a palindrome"
11 11 -> "It's a palindrome"


Here you have it, my friend. There are many ways on how to approach this problem and I believe that's the beauty of it. Have fun figuring out and building your own palindrome checker to put to practice what you've learned today :]


As always thank you SOOO much for reading!! :)

I really hope you learned something new today!

Don't hesitate to contact me and let me know what you think in the comments!

☕If you enjoy my content Buy me a coffee It'll help me continue making quality blogs💕

💙Follow me on Twitter to know more about my self-taught journey!

💜Check out more articles on my JavaScript For Newbies Series

💜 Web Design for Beginners Series

❤️ Also subscribe to my Youtube Channel !

🖤And for more content and tips on TikTok !