Check If A String Is A Palindrome in JS

I'm a full stack developer based in Australia. I write articles about Node.js, React, JavaScript & Web Development to make your learning journey easier ๐
Search for a command to run...

I'm a full stack developer based in Australia. I write articles about Node.js, React, JavaScript & Web Development to make your learning journey easier ๐
No comments yet. Be the first to comment.
Introduction Website performance is an important aspect of web development. It consists of mainly two things: The speed of your website How usable is your website for visitors? Importance of website performance Website performance is very importa...

There are plenty of native web APIs that have a variety of uses. Here are 5 useful APIs that I use quite often in my web development projects. Clipboard API The clipboard API is a simple method to copy text to a user's clipboard. This can be used in ...

Hey All ๐ In this article, we'll be covering the Fetch API in JavaScript how you can use it to make HTTP requests. What is the Fetch API? The fetch() method provides a simple way to make HTTP requests in JavaScript. It is natively in the browser, me...

Hey All ๐ In this article, we'll be covering 4 mistakes commonly made by people while learning to code that will seriously impact their coding journey. #1 - Copying and Pasting The first mistake I'll talk about is copying and pasting. We've all don...

Introduction There are plenty of units in CSS. From px to vh, there is an abundance of units all with their distinctive use cases. Considering the amount of options, its perfectly reasonably to be confused as to which unit you should use and where. T...

In this article, I'll be teaching you how to check if a string is a palindrome in JavaScript.
Lets start off by describing what a palindrome actually is.
A palindrome a word that reads the same backwards as forwards. Pretty simple right? You've probably heard of words like racecar and level which are both palindromes because when you read them backwards, they spell the exam same word.
In layman's terms, to check if a string is a palindrome, we have to reverse it and check if the reverse is equal to the original. To actually code this, lets write a simple JavaScript function that takes in a str parameter and returns a Boolean that signifies whether the passed in str is a palindrome or not.
Lets call this function checkPalindrome:
const checkPalindrome = str => {
// Return Boolean
}
Firstly, we will reverse the string. To reverse the string, we will first split it into an array, reverse the array and then join it back into a string. We can use the .split, .reverse and .join methods respectively:
const checkPalindrome = str => {
let reversedStr = str.split("").reverse().join("");
}
The empty quotes (
"") passed into the split and join methods means it will split each letter separately and not add a space when joining them back together.
We can then return reversedStr == str which return true if its a palindrome and false if its not:
const checkPalindrome = str => {
let reversedStr = str.split("").reverse().join("");
return reversedStr == str;
}
We now have a working checkPalindrome function that if you test with a word like racecar will work.
One problem is that if a single letter in the word is uppercase, it will return false since its case-sensitive. We can fix this bug by converting both strings (reversed and original) to lowercase:
const checkPalindrome = str => {
let reversedStr = str.split("").reverse().join("");
return reversedStr.toLowerCase() == str.toLowerCase();
}
Done! ๐
You've now learned how to check if a string is a palindrome in JavaScript. This solution is very simple, however it doesn't take into account punctuation and whitespace. Both of which would lead to more steps in the solution.
๐ Thanks for reading this article!
If you like what I do and would love to see more related content, follow me on my other social platforms:
GitHub: Blake-K-Yeboah
LinkedIn: Blake-K-Yeboah
You can also show your support by buying me a coffee ๐