JavaScript

How to check if a string is a palindrome or not in JavaScript

Tackling strings in programming presents us with many different opportunities where multiple different actions can be done on the string. Such as finding out its length, searching for a keyword amongst the string, and even checking to see if that string is a palindrome or not. A palindrome is a string that will remain the same if the string gets inverted. For example, if we take the string “rotator” and invert it, it will return us the exact same string.

In this article, it will be shown how someone can implement the concept of palindrome within the code of JavaScript by creating a check for it.

How to verify a palindrome using JavaScript?

There are two major methods through which anyone can verify whether a particular string is a palindrome or not using JavaScript. This section elaborates on the functionality of the methods that are given below.

By Dividing the String

The first method that exists involves dividing the given string into two halves. Let’s dive deeper to gain a better understanding of this algorithm. The code is given below:

functioncheckPalindrome(){
//section 1
var palString = "dumb";  
var len = palString.length;  

//section 2  
for (let i = 0; i < len / 2; i++) {  

//section 3  
if (palString[i] !== palString[len - 1 - i]) {  
            alert( 'It is not a palindrome');
return
        }
    }    
    alert( 'It is a palindrome');
}
console.log(checkPalindrome());

To make it easier for you to understand, the code is divided into 3 sections. Let's try to gain an understanding of them one by one.

Section 1: Getting String Length

In the first section a variable is declared in which the value of the string is stored. You can take a user input or hard code the word yourself. Using the length keyword, the length of the string is calculated and stored in the variable len.

Section 2: Dividing the String

The string is divided into two halves using the condition in the for loop. This means that the loop will run for only the first half.

Section 3: Compare both halves

Using an if statement, the first half run by the for loop is checked against the second half. If the letters in the string do not match, the function terminates after telling us that it is not a palindrome. Otherwise, if the loop runs all the way through, this means that the string is a palindrome.

Below is a snippet of how the process works:

Using Built-in Functions

The second technique is much easier to execute compared to the first. It requires the use of some built-in JavaScript functions. Below is the implementation of how these functions work:

functioncheckPalindrome ()  {
var string = "notpal";
var arrayString = string.split (''); //  
const reverseArray = arrayString.reverse();  
const reverseString = reverseArray.join('');  
if (string == reverseString)  {  
        alert('This String is a Palindrome');
    }  
else {  
        alert ('This String is NOT a Palindrome' );  
    }  
}
console.log(checkPalindrome());

This code makes use of the split() function which will convert the String into an array by splitting each character. Once an array, it will simply be reversed using the reverse() function as shown above. We will change the reversed string back into an array. To achieve this the join() function is going to be used. Once both the string and the reverse string are obtained, they are compared and if they turn out to be equal then the string is a palindrome. An example of this is shown below:

In this example, it is very clear that the string “notpal” is not a palindrome and the program shows the output.

Conclusion

There are two ways in which palindrome can be checked. The first way involves dividing the string into two halves and comparing one half with the other. The second way involves the use of some in-built functions which convert the string into an array and reverse it so that the reversed string can be compared with the original string.

About the author

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.