Signup/Sign In

Check for Palindrome in JavaScript - #3 Different Ways

Posted in Tricks   LAST UPDATED: JULY 11, 2023

    In this post, we will be learning how to write a simple JavaScript function to find if the given string is a palindrome or not. And we will learn not just one way but 3 different ways of doing so. For those who do not know what a palindrome is, a palindrome is a string, sequence of numbers, or characters that reads the same backward as forward, for example, madam, nitin, 12321, etc.

    The Palindrome Problem

    Given a string, return true if the string is a palindrome or false if it is not.

    Palindromes are strings that form the same word if it is reversed. *Do* include spaces and punctuation in determining if the string is a palindrome.

    For Example:

    palindrome("abba") === true
    palindrome("abcdefg") === false


    Solution for the Palindrome Problem:

    First, try to solve it by yourself then come to the solution.

    Solution number 1

    In this approach, we will use a basic loop for finding out if the given string is a palindrome or not.

    function palindrome(str) {
        let i = 0;
        let j = str.length - 1;
        while(i < j) {
            if(str[i] == str[j]) {
                i++; 
                j--;
            }
            else {
                return false;
            }
        }
        return true;
    }

    Solution number 2

    In this approach, we will use the every() function for iterating over the characters of the string and comparing it with its opposite end character.

    function palindrome(str) {
        return str.split('').every((char, i) => {
            return char === str[str.length - i - 1];
        });
    }

    Solution number 3

    In this approach, we will use split(), reverse() and join() method to reverse the string and then compare it with the original string to see if they are the same or not. If found the same, that means the string in question is a palindrome, else not.

    function palindrome(str) {
        const reversed = str
        .split('')
        .reverse()
        .join('');
    
        return str === reversed;
    }

    Wow, we now know, 3 different ways to solve the palindrome problem in JavaScript.

    Conclusion

    Congratulations! You have now explored three different approaches to check for palindromes in JavaScript. Whether you used string reversal, iteration, or regular expressions, each method offers a unique way to identify palindromes and improve your problem-solving abilities.

    Frequently Asked Questions(FAQs)

    What is a palindrome?

    A palindrome is a word, phrase, number, or sequence of characters that reads the same forwards and backward. For example, "racecar" and "madam" are palindromes.

    How can I check for a palindrome in JavaScript using string reversal?

    To check for a palindrome using string reversal, you can reverse the input string and compare it to the original string. If both are the same, the string is a palindrome.

    Can I check for a palindrome in JavaScript using iteration?

    Yes, you can check for a palindrome using iteration. By comparing characters from the start and end of the string and moving toward the middle, you can determine if it is a palindrome.

    Is it possible to check for a palindrome in JavaScript using regular expressions?

    Yes, regular expressions can be used to check for palindromes in JavaScript. By removing non-alphanumeric characters and converting the string to lowercase, you can compare it with its reversed form using regular expressions.

    Which approach is the most efficient for checking palindromes in JavaScript?

    The efficiency of different approaches can vary depending on the size of the input string and the specific implementation. In general, the iterative approach tends to be more efficient, followed by string reversal and regular expressions. However, the differences may be negligible for smaller input sizes, so choosing an approach should also consider simplicity and code readability.

    You may also like:

    About the author:
    Incoming Software Engineer @Vedantu, Codeforces (1765, expert). Former Summer Intern @Wikimedia Foundation(GSoC), @Egnify, @Vedantu.
    Tags:programmingjavascript
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS