How to useString Reversal in Javascript
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.
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
Output
true false
JavaScript Program to Check for Palindrome Number
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:
Table of Content
- Approach 1: Using String Reversal
- Approach 2: Using Array Every() Method
- Approach 3: Using XOR Operator
- Approach 4: Using for Loop and Math.floor() Method
- Approach 5: Using Recursion
We will explore all the above methods along with their basic implementation with the help of examples.