Signup/Sign In
PUBLISHED ON: FEBRUARY 15, 2023

JavaScript Program to Check whether a string is Palindrome or not

Palindrome strings are words or phrases that have the same meaning when read from left to right and right to left. Checking whether a string is a palindrome is a common programming problem that can be solved in many ways. To check whether a string is a palindrome, you can use built-in functions, loops, or recursion in JavaScript. In this program, we will look at various JavaScript methods to determine whether a given string is a palindrome or not.

This program will teach you how to check for palindrome strings using JavaScript, whether you are a beginner or an experienced programmer. We also have an interactive JavaScript course where you can learn JavaScript from basics to advanced and get certified. Check out the course and learn more from here.

What is a Palindrome Number?

A palindrome is a word, number, phrase, or other sequence of symbols that reads the same backward as well as forward. For example, madam reads the same either from forward or backward. So the word madam is a palindrome.

Approach

Here, we have two ways to find the Palindrome of a word and check whether it is a palindrome or not.

Program to check whether a string is Palindrome or not

  • The for loop is used to iterate up to half of the string and the if the condition is used to check if the first and the corresponding last characters are the same. This loop continues till the half length of the string.
  • Throughout this process, if any character of the string, when compared with its corresponding last string is not equal, the string is not considered a palindrome.
// JS Program to check if the string is palindrome or not
function checkPalindrome(string) {

    let l = string.length;

    // loop running through half of the string
    for (let i = 0; i < l / 2; i++) {

        // checking the index in the first half with the index in the second half
        if (string[i] !== string[len - 1 - i]) {
            return "It is not a palindrome";
        }
    }
    return "It is a palindrome";
}

const word = prompt('Enter a string: ');

console.log(checkPalindrome(word));

?


Enter a string: madam
It is a palindrome

Program to check whether a string is Palindrome or not using built-in Functions

The split('') method basically converts the string into individual characters.

The reverse() method reverses the position in all the elements of the array.

The join('') method joins all the elements of an array into a defined string.

// JS Program to check if the string is palindrome or not

function checkPalindrome(string) {

    // convert string to an array
    const arrayVals = string.split('');

    // reverse 
    const reverseArrayVals = arrayVals.reverse();

    // convert array to string
    const reverseString = reverseArrayVals.join('');

    if(string == reverseString) 
        console.log('It is a palindrome');
    
    else 
        console.log('It is not a palindrome');
}

const word = prompt('Enter a string: ');

checkPalindrome(word);


Enter a string: world
It is not a palindrome

Conclusion

Finally, determining whether a string is a palindrome is a common programming problem, and JavaScript provides several solutions. In this program, we used JavaScript to investigate various methods for determining whether a given string is a palindrome or not.



About the author:
Proficient in the creation of websites. Expertise in Java script and C#. Discussing the latest developments in these areas and providing tutorials on how to use them.