Javatpoint Logo
Javatpoint Logo

Palindrome in JavaScript

In this topic, we will learn about Palindrome and validate that the given numbers or strings are Palindrome or not in JavaScript.

Palindrome in JavaScript

A palindrome is used to verify a sequence of numbers, strings, or letters that are read left to right and right to left to match the same characters or return the same sequence of characters. In simple terms, when the numbers, strings, or characters are reversed that return the same result as the original numbers or characters, it is called a Palindrome. For example, NITIN, 123454321, madam, etc. Suppose we have a word, madam. When we read the word madam from the forward and the backward end, it returns the same string. Therefore, we can refer to the string or number as the Palindrome.

Palindrome Algorithm

Following are the steps to get the Palindrome in JavaScript, as follow:

  1. Get the strings or numbers from the user.
  2. Take a temporary variable that holds the numbers.
  3. Reverse the given number.
  4. Compare the original number with the reversed number
  5. If the temporary and original number are same, it the number or string is a Palindrome.
  6. Else the given string or number is not the Palindrome.

Check Palindrome number and string by accepting the number through prompt box.

string.html

Test it Now

Output

When the above code is executed, it shows the below image:

Palindrome in JavaScript

Now we enter the number or string on the prompt alert box and then click the OK button to validate the given string is Palindrome or not.

Palindrome in JavaScript

After that, it shows the below Output.

Palindrome in JavaScript

If the given string is not a Palindrome, it shows the string is not a Palindrome. Similarly, we can enter the number and validate whether the given number is Palindrome.

Check Palindrome string using built-in Functions

In this program, we use the built-in function like the split () method, reverse () method, and join () method to find the Palindrome of the number or string. Let's consider a JavaScript program to find the Palindrome of the given number using the built-in function.

Program2.html

Test it Now

Output

When we execute the above code, it shows the below image:

Palindrome in JavaScript

In the above images, we entered a string MADAM and then click the OK button to check the given string is a Palindrome or not.

After that, it shows the below image:

Palindrome in JavaScript

Following are the built-in function to check the palindrome in JavaScript.

  1. The split (' ') function is used to convert the string into the individual array characters.
    const arrayChar = string.split( ' '); // ["A", "P", "P", "L", "E"]
  2. A reverse () function helps to reverse the position of the individual array characters.
    const reverseArray = arrayChar.reverse (); // ["E", "L", "P", "P", "A"]
  3. The join () method is used to combine the individual array characters into the string.
    const reverseString = reverseArray.Join (' '); // return string is: "ELPPA"

Check the Palindrome Number in JavaScript

Let's create a program to check whether the entered number is a Palindrome in JavaScript.

Str.html

Test it Now

Output

When the above programming code is executed, it returns the below images:

Palindrome in JavaScript

We enter the numbers and click on the Check button to validate the given number is Palindrome, as shown below.

Palindrome in JavaScript

In the above image, it shows that "It is a Palindrome Number." And if the reversed number is not the same as the original number, it shows that the entered number is not a Palindrome.

Check the Palindrome String in JavaScript

Let's create a program to check whether the entered string is a Palindrome in JavaScript.

Str2.html

Test it Now

Output

When the above programming code is executed, it returns the below image:

Palindrome in JavaScript

We entered the string "MADAM" and then click on the Submit button in the above output. It displays the below result, as shown:

Palindrome in JavaScript

Explanation of the code: When we entered the "MADAM" String in the textbox and then click on the Submit button. After that, the original string "MADAM" is reversed and compares each string's character with the original one. If both strings are equal, it shows that "It is a Palindrome String".

Palindrome in JavaScript

In the above output, when we entered the string JOHN, it displays the string is not a Palindrome string as it does not match the reversed characters of the string with the original.






Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA