# Check If A String Is A Palindrome in JS

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.

## What Is A Palindrome?

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.

## How To Check If A String Is A Palindrome

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`:

```javascript
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:

```javascript
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:

```javascript
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:


```javascript
const checkPalindrome = str => {
    let reversedStr = str.split("").reverse().join("");
    return reversedStr.toLowerCase() == str.toLowerCase();
}
```

Done! 😀

## Take Away

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](https://github.com/Blake-K-Yeboah)

**LinkedIn:** [Blake-K-Yeboah](https://www.linkedin.com/in/blake-yeboah/)

You can also show your support by buying me a coffee 😃
 
<a href="https://www.buymeacoffee.com/blakeyeboah"><img src="https://img.buymeacoffee.com/button-api/?text=Buy me a coffee&emoji=&slug=blakeyeboah&button_colour=855ff6&font_colour=ffffff&font_family=Cookie&outline_colour=ffffff&coffee_colour=FFDD00"></a>





