DEV Community

Cover image for Numerical Palindrome in JavaScript
coder4life
coder4life

Posted on

Numerical Palindrome in JavaScript

A palindrome is a word, number, or sequence of characters which reads the same backward as forward. In this coding challenge we need a solution that tells if a given number is a palindrome. Only numbers allowed. For example: 2442 or 100001.

function isPalindrome(num) {
    if (typeof num !== 'number' || num < 0) {
        return false;
    }

    return +num.toString().split('').reverse().join('') === num;
}

console.log(isPalindrome(2442)); // true
console.log(isPalindrome(1234)); // false
console.log(isPalindrome(100001)); // true

Video going through this example:

Top comments (0)