Palindrome in JavaScriptIn this topic, we will learn about Palindrome and validate that the given numbers or strings are Palindrome or not 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 AlgorithmFollowing are the steps to get the Palindrome in JavaScript, as follow:
Check Palindrome number and string by accepting the number through prompt box.string.html Test it NowOutput When the above code is executed, it shows the below image: ![]() 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. ![]() After that, it shows the below Output. ![]() 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 FunctionsIn 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 NowOutput When we execute the above code, it shows the below image: ![]() 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: ![]() Following are the built-in function to check the palindrome in JavaScript.
Check the Palindrome Number in JavaScriptLet's create a program to check whether the entered number is a Palindrome in JavaScript. Str.html Test it NowOutput When the above programming code is executed, it returns the below images: ![]() We enter the numbers and click on the Check button to validate the given number is Palindrome, as shown below. ![]() 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 JavaScriptLet's create a program to check whether the entered string is a Palindrome in JavaScript. Str2.html Test it NowOutput When the above programming code is executed, it returns the below image: ![]() We entered the string "MADAM" and then click on the Submit button in the above output. It displays the below result, as shown: ![]() 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". ![]() 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.
Next TopicJavaScript Call Stack
|