Palindrome check in Javascript

Maybe I will suggest alternative solution:

function checkPalindrom (str) {
  return str == str.split('').reverse().join('');
}

UPD. Keep in mind however that this is pretty much “cheating” approach, a demonstration of smart usage of language features, but not the most practical algorithm (time O(n), space O(n)). For real life application or coding interview you should definitely use loop solution. The one posted by Jason Sebring in this thread is both simple and efficient (time O(n), space O(1)).

Leave a Comment