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 :
- Read the input string as parameter.
- Convert the input string as array.
- Reverse the array and convert to string.
- 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");