Categories
JavaScript Answers

How to check for palindrome in JavaScript?

Spread the love

Sometimes, we want to check for palindrome in JavaScript.

In this article, we’ll look at how to check for palindrome in JavaScript.

How to check for palindrome in JavaScript?

To check for palindrome in JavaScript, we check if the string and the reversed version of the string is the same.

For instance, we write

const checkPalindrome = (str) => {
  return str === str.split("").reverse().join("");
};

to define the checkPalindrome function.

In it, we get the reversed string with str.split("").reverse().join("").

And we check if it’s the same as the original str string with ===.

Conclusion

To check for palindrome in JavaScript, we check if the string and the reversed version of the string is the same.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *