JavaScript Program to Check Whether a String is Palindrome or Not

To understand this example, you should have the knowledge of the following JavaScript programming topics:


A string is a palindrome if it is read the same from forward or backward. For example, dad reads the same either from forward or backward. So the word dad is a palindrome. Similarly, madam is also a palindrome.


Example 1: Check Palindrome Using for Loop

// program to check if the string is palindrome or not

function checkPalindrome(string) {

    // find the length of a string
    const len = string.length;

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

        // check if first and last string are same
        if (string[i] !== string[len - 1 - i]) {
            return 'It is not a palindrome';
        }
    }
    return 'It is a palindrome';
}

// take input
const string = prompt('Enter a string: ');

// call the function
const value = checkPalindrome(string);

console.log(value);

Output

Enter a string: madam
It is a palindrome

In the above program, the checkPalindrome() function takes input from the user.

  • The length of the string is calculated using the length property.
  • The for loop is used to iterate up to the half of the string. The if condition is used to check if the first and the corresponding last characters are the same. This loop continues till the half of the string.
  • During the iteration, if any character of the string, when compared with its corresponding last string is not equal, the string is not considered a palindrome.

Example 2: Check Palindrome using built-in Functions

// program to check if the string is palindrome or not

function checkPalindrome(string) {

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

    // reverse the array values
    const reverseArrayValues = arrayValues.reverse();

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

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

//take input
const string = prompt('Enter a string: ');

checkPalindrome(string);

Output

Enter a string: hello
It is not a palindrome

In the above program, the palindrome is checked using the built-in methods available in JavaScript.

  • The split('') method converts the string into individual array characters.
    const arrayValues = string.split(''); // ["h", "e", "l", "l", "o"]
  • The reverse() method reverses the position in an array.
    // ["o", "l", "l", "e", "h"]
    const reverseArrayValues = arrayValues.reverse();
  • The join('') method joins all the elements of an array into a string.
    const reverseString = reverseArrayValues.join(''); // "olleh"
  • Then the if...else statement is used to check if the string and the reversed string are equal. If they are equal, the string is a palindrome.

Note: The multiple lines of code can be reduced and written in one line:

const reverseString = string.split('').reverse().join('');

Also Read:

Before we wrap up, let’s put your knowledge of JavaScript Program to Check Whether a String is Palindrome or Not to the test! Can you solve the following challenge?

Challenge:

Write a function to find whether a string is palindrome.

  • A palindrome is a string that reads the same forwards and backwards. For example: "radar", "madam" and so on.
  • Return true if the input string is a palindrome. Otherwise, return false.
Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges