OneBite.Dev - Coding blog in a bite size

check if a string is a palindrome in Javascript

Code snippet on how to check if a string is a palindrome in Javascript

function isPalindrome(str) {
  let reversedStr = str.split('').reverse().join('');
  
  if (str === reversedStr) {
    return true;
  } else {
    return false;
  }
}

This code checks whether a string is a palindrome or not. A palindrome is a type of word or sentence that is the same when read forwards and backwards. The code first splits the string into an array of characters, then reverses the order of the characters, and then joins them back together. It then compares this reversed string with the original string. If they are the same it will return true, otherwise false. This can be used to test if a given word or sentence is a palindrome.

javascript