JavaScript(JS) JS check whether a string is palindrome or not
To check if a string is a palindrome, you need to check if it reads the same backward as forward. Here's one way to do it in JavaScript:
function isPalindrome(str) {
// convert string to lowercase and remove non-alphanumeric characters
str = str.toLowerCase().replace(/[\W_]/g, '');
// reverse the string
var reversedStr = str.split('').reverse().join('');
// check if the original string and the reversed string are the same
return str === reversedStr;
}
// test the function
console.log(isPalindrome("A man, a plan, a canal, Panama!")); // true
console.log(isPalindrome("Hello, world!")); // falseSource:www.theitroad.comIn this implementation, the isPalindrome() function takes a string as input, converts it to lowercase, and removes all non-alphanumeric characters using a regular expression. Then, it splits the string into an array of characters, reverses the order of the characters, and joins them back into a string. Finally, it compares the original string and the reversed string, and returns true if they are the same, and false otherwise.
