Vanilla JavaScript palindrome checker in 3 lines

Chris Bongers - Oct 10 '20 - - Dev Community

There comes a time in your life where you need a palindrome checker!

You might now think, what is a palindrome?

It's a word or sentence like mom that you can reverse, and it's still mom!

If that time comes, think about this article and how to check for palindromes in JavaScript.

We will be building this amazing palindrome function, try it out on my Codepen. (See console logs!)

JavaScript palindrome function

To create our function we define a function that accepts one argument, a string.

function palindrome(string) {
    // Code here
}
Enter fullscreen mode Exit fullscreen mode

Then we need to convert our input string to lowercase and remove all whitespace.

const original = string.replace(/\s/g,'').toLowerCase();
Enter fullscreen mode Exit fullscreen mode

We are using a regular expression to remove all whitespaces.

The next step is to get the reverse of our string.
We we split every character and reverse the array, then we rejoin that array in reversed order.

const reverse = original.split('').reverse().join('');
Enter fullscreen mode Exit fullscreen mode

The last step is to check if they are equal.

return original === reverse;
Enter fullscreen mode Exit fullscreen mode

The whole function will look like this.

function palindrome(string) {
  const original = string.replace(/\s/g,'').toLowerCase();
  const reverse = original.split('').reverse().join('');
  return original === reverse;
}
Enter fullscreen mode Exit fullscreen mode

Awesome, let's see how it works in action.

console.log(palindrome('Mom')); // True
console.log(palindrome('A nut for a jar of tuna')); // True
console.log(palindrome('Not a palindrome')); // False
console.log(palindrome('Taco cat')); // True
console.log(palindrome('Yo banana boy')); // True
Enter fullscreen mode Exit fullscreen mode

Great stuff, we now have a palindrome checker in JavaScript!

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player