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.