Skip to content
Related Articles

Related Articles

How to check whether a passed string is palindrome or not in JavaScript ?

View Discussion
Improve Article
Save Article
  • Last Updated : 29 Aug, 2022
View Discussion
Improve Article
Save Article

Given a string, our task is to find string is palindrome or not.

Example:
Input : "race"
Output : passed string is not a palindrome
Explanation : if we write "race" in reverse that is "ecar" it not 
matches with first string so it is not a palindrome.
Example 2:
Input : "hellolleh"
Output : passed string is palindrome.

Approach 1: In this approach we use following steps.

  • First we iterate over a string in forward and backward direction.
  • Check if all forward and backward character matches, return true.
  • If all forward and backward character does not matches, return false.
  • If return is true, it is a palindrome.

Example: 
 

Javascript




<script>
  // function that check str is palindrome or not
  function check_palindrome( str )
  {
    let j = str.length -1;
    for( let i = 0 ; i < j/2 ;i++)
    {
      let x = str[i] ;//forward character
      let y = str[j-i];//backward character
      if( x != y)
      {
        // return false if string not match
        return false;
      }
    }
    /// return true if string is palindrome
    return true;
     
  }
 
  //function that print output is string is palindrome
  function is_palindrome( str )
  {
    // variable that is true if string is palindrome
    let ans = check_palindrome(str);
    //condition checking ans is true or not
    if( ans == true )
    {
      console.log("passed string is palindrome ");
    }
    else
    {
      console.log("passed string not a palindrome");
    }
  }
  // test variable
  let test = "racecar";
  is_palindrome(test);
</script>


 
Output :

passed string is palindrome.

Approach 2: Another approach is to reverse a string and check if the initial string matches with the reverse string or not.

Follow the following steps : 

  • Initialize reverse_str a variable which stores reverse of the passed string.
  • Compare the string to reverse_str .
  • If matches, it is a palindrome.
  • Else string is not a palindrome.

Example: 

Javascript




<script>
  // function to reverse the string
  function reverse( str )
  {
    // variable holds reverse string
    let rev_str = "";
    for( let i = str.length-1 ;i >= 0 ;i--)
    {
      rev_str+= str[i];
    }
    // return reverse string
    return rev_str;
  }
 
  //  function checking string is palindrome or not
  function is_palindrome( str )
  {
    reverse_str = reverse(str);
    //  condition checking if reverse str is
    // same as string it is palindrome
    // else not a palindrome
    if( reverse_str === str)
    {
      console.log("passed string is palindrome ");
    }
    else
    {
      console.log("passed string is not palindrome")
    }
  }
  let test = "hellolleh";
  is_palindrome(test);
</script>


 
Output :

passed string is palindrome.

Approach-3: Another approach, which is though the shortest approach, uses split(), reverse() and join() method.

  • Split the string of characters into several different characters (which is though unsorted at the moment).
  • Use reverse() method to reverse all the characters of the string alphabetically.
  • Then apply join() method in order to join all the characters of string (which are now sorted).

Below is the implementation of the above approach:

Javascript




// JavaScript code in order to check string palindrome...
 
let checkPalindrome = (stringg) => {
  return stringg === stringg.split("").reverse().join("");
};
 
console.log("Is Palindrome? : " + checkPalindrome("noon"));
console.log("Is Palindrome?: " + checkPalindrome("apple"));
 
// This code is contributed by Aman Singla...


Output:

Is Palindrome? : true
Is Palindrome?: false

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!