Palindrome number in JavaScript
In this post, you will learn how to write program to check whether a number is palindrome or not in JavaScript.
A palindrome is a number, string, or sequence that remains the same when it is reversed. A palindrome sequence can be a combination of digits or numbers, but it looks the same going forward and backward. It is generally used where recreational mathematics is applied, like puzzles and games. To check whether a number or string is a palindrome or not, simply reverse it and compare it with the original number or string. If both are the same, then the number or string is a palindrome, otherwise not.
121, 1991, 1239321, MAAM, DAD, WOW
When we reverse the above numbers or strings, they will remain the same. So, all the above are palindromes.
These are different programming ways in JavaScript to check for palindrome numbers and strings-
Check Palindrome using built-in functions in JavaScript
In the given JavaScript code, we have used several built-in methods to check for palindrome. The split() method splits a string object into an array of strings by separating the string into sub strings. The reverse() method reverses an array in place. The first array element becomes the last, and the last becomes the first. The join() method joins all array elements into a string. Then, we compare it with the original string. If equal, it means the string is a palindrome.
<html>
<head>
<title> JavaScript Palindrome </title>
</head>
<body>
<script>
function Check_Palindrome(string)
{
// using string.split() function
// convert the string into an array
const arrVal = string.split ('');
// reverse the array values using reverse() method
const revArrVal = arrVal.reverse();
// grouping the array values into the string
// using join() function
const reverseStr = revArrVal.join('');
// Check Palindrome
// if string condition is equal to the reverseStr
if (string == reverseStr)
{
alert('It is a Palindrome string.');
}
else {
alert('It is not a Palindrome string.');
}
}
// Taking a string from the user
const string = prompt( 'Please enter a string');
const value = Check_Palindrome (string);
console.log(value);
</script>
</body>
</html>
Output of the above code:
In the below prompt box, we enter 'MADAM' and click the OK button.
It shows the given output.
Check Palindrome using for Loop in JavaScript
In the given JavaScript program, first we take input from the user and pass it as an argument to the checkPalindrome() function. Within the function, first we calculate the length of the string using the length property. The for loop is used to iterate up to half the string. The if condition is used to check if the first and the corresponding last characters are the same. The loop continues till the half of the string. In comparison, if the corresponding last string is not equal, the string is not considered a palindrome.
<script>
// program to check if the string is palindrome or not
function checkPalindrome(string) {
// find the length of string
const len = string.length;
// loop through half of the string
for (let i = 0; i < len / 2; i++) {
// check if first and last string are same
if (string[i] !== string[len - 1 - i]) {
alert('It is not a Palindrome string.');
}
}
alert('It is a Palindrome string.');
}
// Taking a string from the user
const string = prompt( 'Please enter a string');
const value = checkPalindrome (string);
console.log(value);
</script>
Related Articles
Generate random numbers in JavaScriptJavascript window location
Remove duplicates from array Javascript
Scientific calculator JavaScript
How to reverse string in Javascript
JavaScript display PDF in the browser using Ajax call
Parsing JSON in Javascript
Javascript speech recognition example
Select/deselect all checkboxes using Javascript
Print specific part of a web page in javascript
Dynamically Add/Delete HTML Table Rows Using Javascript
jQuery Ajax serialize form data example
Image popup on page load using HTML and jQuery
Ajax live data search using jQuery PHP MySQL
jQuery loop over JSON result after AJAX Success
Simple star rating system using PHP, jQuery and Ajax
jquery image zoom on mouseover example
Bootstrap star rating input example
Bootstrap datepicker example
Submit a form data without page refresh