Validate palindromes with javascript 💻 2022

verificar palindromos con javascript | discover palindromes with javascript code

23 September 2020

Comparte | Share

Validate Palindromes with Javascript code

In this post we will know how to validate palindromes with javascript code. Firstly we should begin by explaining, what the term palindrome specifically mean? Well, no more, no less that we can read text or words from left to right as well as from right to left without changing its meaning.

This type of exercise is usually part of 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.


var cadena = prompt('Introduzca un texto o palabra por favor');
cadena = cadena.toLowerCase().replace(/\s/g,"");
var reves = cadena.split("").reverse().join("");

function palindromo (cadena) {
    if(cadena == reves){
      alert('El texto o palabra introducido es un palindromo');
      alert('El texto o palabra introducido NO es un palindromo');
palindromo (cadena);


Verify palindromes with Javascript code step by step

Firstly we create a variable called cadena. In this variable, we will see the option prompt. When the prompt option is used the browser will give us the possibility to insert text into a small pop-up. Therefore the information that the user enters in this pop-up will be stored in the variable cadena.

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.

With the replace ( ) method, we remove the empty spaces that the text inserted by the user may have.

As a result of those two methods, toLowerCase() and replace(), we are converting what the user has written in text without spaces and in lower case.

This format will help us in the comparison to validate palindromes with javascript code and we avoid the potential “case sensitive” and space errors of letters or names.

We also create another variable called reves. This variable converts the information stored in the variable cadena from right to left.

After this, we are ready to compare the values of cadena and reves.

Finally, this comparison is carried out by the function palindrome. If the value of both of them does not match, then an unsuccessful message will come up. However, if the value matches with each other, we will get a successful message and we can happily say that we have verified palindromes with javascript code.

We are a web desin company based in Canarias. This post is an HTML exercise that the students usually encounter in their proyects.

Comparte | Share
creatuwebpymes quien somos

Francisco Brito Diaz

CEO of, web design company & digital marketing in Canary Islands.