Check Palindrome Using JavaScript

How To Check Palindrome Using JavaScript

In this tutorial, you’ll learn how to implement a program to check palindrome using JavaScript.

What Is Palindrome ?

A string or phrase which reads the same when read backward or forward. An example of a palindrome string will be MALAYALAM, NOON.

Implement Palindrome Check

Implementing Palindrome check in JavaScript will be a 3 step process :

  1. Read the input string as parameter.
  2. Convert the input string as array.
  3. Reverse the array and convert to string.
  4. Compare the input string and the reversed string, if same, you have a palindrome.

Here is how the JavaScript function to check palindrome looks :

function checkPalindrome(input) {
  input_array = input.split("");
  let output = input_array.reverse().join("");
  if (input == output) {
    console.log(input, " is palindrome");
  } else {
    console.log(input, " is not palindrome");
  }
}

checkPalindrome("MALAYALAM");
checkPalindrome("GOD");
checkPalindrome("NOON");