Implement isPalindrome in JS

Sunday 24/07/2022

·1 min read
Share:

To implement isPalindrome function we can achieve it by implementing another helper function reverseStr().

function reverseStr(str) {
	return str.split('').reverse().join('')
}
  • split('') - will separate every char to array element
  • reverse() - is a method on arrays that reverses the order of the elements
  • join('') - will join array elements to string

And now we can use it for our isPalindrome()

function isPalindrome(str) {
	return str === reverseStr(str)
}
Share:
VA

Vadim Alakhverdov

Software developer writing about JavaScript, web development, and developer tools.

Related Posts