JavaScript Program to Check Whether a String is Palindrome String or Not

Problem Statement 

A string is generally called a palindrome string when the reverse of the string is the same as the original string. For example dad, level, and noon.

Example 1:
Input: Str =  “LEVEL”
Output: True // Which means the string is palindrome
Explanation: String when reversed is the same as string.

Example 2:
Input: Str = “AJAY”
Output: False // Which means that the string is not palindrome
Explanation: String when reversed is not the same as string.

Let us take an example where we will learn to write a javascript program that will check if the string is palindrome or not. In the first example, we will generally implement the logic by using inbuilt javascript functions.

Example 1

Palindrome String Example

Output 1:

Palindrome String Output in Javascript

In the above example, we have created isPal() function and in it, we are trying to first split the string and then reverse it. After reversing the string we are checking whether the reverse string is the same as the original string or not. If both are the same then we are console logging true on the screen otherwise we are just displaying false on the screen.

Example 2:

Palindrome String Example 2

Output 2:

Palindrome String Output in Javascript


In the above example, we have initialized the new string with the value of the old string and just converted the old string to lowercase. Other than that we have also taken two variables which are left and right. The left variable has been initialized with zero and the right variable is initialized with the value (length of string – 1). This is done to compare the first character of the string with the last character for the first iteration. If the first character is not equal to the last character then we are just returning false and incrementing the left value by 1 and decrementing the right by 1. Otherwise, we are just returning true if they both are the same as it generally denotes that the string is Palindrome.

Related Articles


Please enter your comment!
Please enter your name here

Stay Connected

- Advertisement -spot_img

Latest Articles