I'm learning JS and had to come up with a function to check if a certain string is a palindrome. I managed to get it right with:
function isPalindrome(word) {
return word == word.split('').reverse().join('');
}
However, my first attempt was:
function isPalindrome(word) {
return word.split("") === (word.split("").reverse());
}
But this does not work. What this second function does is get the string and make an array out of it, then compare that to the string as an array but reversed. If I console.log()
both sides I get the same array (in the case of a palindrome like "level") so why does this always evaluate to false?