In this article, we are going to learn about Palindrome Numbers in JavaScript. A palindrome number is a numerical sequence that reads the same forwards and backward, It remains unchanged even when reversed, retaining its original identity.
Example:
Input : Number = 121
Output : Palindrome
Input : Number = 1331
Output : Palindrome
There are several methods that can be used to check if a number is a Palindrome Number in JavaScript, which are listed below:
- Using String Reversal
- Using Array Every() method
- Using XOR Operator
- Using for Loop and Math.floor() Method
We will explore all the above methods along with their basic implementation with the help of examples.
Approach 1: Using String Reversal
In this approach, we Convert the number to a string. Reverse the string and compare it with the original. Return true if equal, indicating a palindrome; otherwise, false.
Syntax:
let result = numStr.split('').reverse().join('');
Example: In this example we are using the above-explained approach.
Javascript
function palindromeCheck(num) { let numStr = num.toString(); let result = numStr.split( '' ).reverse().join( '' ); return numStr === result; } console.log(palindromeCheck(121)); // true console.log(palindromeCheck(123)); // false |
true false
Approach 2: Using Array Every() Method
In this approach, we convert number to string, and split it into array. Check if each digit matches its counterpart in reverse order using the every method for palindrome detection.
Syntax:
every((element, index, array) => { /* … */ })
Example: In this example, the number is converted to a string, and then that string is split into an array of characters. The every method is used to check if every digit in the array matches its counterpart in the reversed position
Javascript
function palindromeCheck(number) { let numStr = number.toString(); let numArr = numStr.split( '' ); return numArr.every((num, index) => num === numArr[numArr.length - 1 - index]); } // Checking the number is Palindrome console.log(palindromeCheck(121)); console.log(palindromeCheck(12321)); console.log(palindromeCheck(12345)); |
true true false
Approach 3: Using XOR Operator
In this approach, we convert number to string. Use XOR on ASCII values of corresponding characters, checking palindrome property. Return boolean value for palindrome detection.
Syntax:
a ^ b
Example:
Javascript
function palindromeCheck(number) { let numStr = number.toString(); let length = numStr.length; let result = 0; for (let i = 0; i < Math.floor(length / 2); i++) { result ^= numStr.charCodeAt(i) ^ numStr.charCodeAt(length - 1 - i); } return result === 0; } // Checking the given number is palidrome or not console.log(palindromeCheck(121)); console.log(palindromeCheck(13531)); console.log(palindromeCheck(12345)); |
true true false
Approach 4: Using for Loop and Math.floor() Method
In this approach, we are using Math.floor method, reverse the number iteratively. Compare reversed number with original for palindrome check. Exclude negatives.
Syntax:
for (let temp = original; temp > 0;
temp = Math.floor(temp / 10)) {
reversed = reversed * 10 + temp % 10;
};
Example: In this example, the palindromeCheck function uses a for loop to reverse positive numbers. It compares the reversed and original numbers, returning true for palindromes and false otherwise.
Javascript
function palindromeCheck(number) { if (number < 0) { return false ; } let original = number; let reversed = 0; for (let temp = original; temp > 0; temp = Math.floor(temp / 10)) { reversed = reversed * 10 + temp % 10; } return number === reversed; } console.log(palindromeCheck(121)); console.log(palindromeCheck(12321)); console.log(palindromeCheck(12345)); |
true true false