In this post we will know how to check palindromes using javascript. By the way, what does the term palindrome specifically mean? Well, no more, no less that we can read that text from left to right as well as from right to left.

This is a kind of javascript exercise that very often appears in the practical tests for javascript students.

With a little bit of javascript code we will be able to know if a word is a palindrome or not. If this text or word is palindrome, then we will get a message confirming so otherwise the message will confirm that it is not.

With the following code we ask the user to enter a word or term. Once it has been entered a pop up message will confirm it or not. To make it easier to verify what the user has entered, the javascript code does not take into account the spaces between words that may be there.

palindromo javascript code


First we create a variable called cadena. In this variable we store what the user enters using a prompt pop up. 

Since we do not know if the user will enter uppercase or lowercase letters, we apply the toLowerCase ( ) method . This method converts the entered text to lowercase letters, in case uppercase letters are entered. Thus, text comparison is easier and we avoid possible “case sensitive” errors of letters or names.

With the replace ( ) method , we remove the empty spaces that the text may have after the user has entered his request.

Then we create another variable called reves. This variable converts the information of the user from right to left the value stored in cadena and this way the two variables can be compared to each other. 

Finally this comparision is carried out by the function palindromo, as it collects the value of cadena and it compares it with the value of reves. If the information entered matches with each other, we will get a succesful message or alternatively an unseccesful message.