Open In App

Palindrome in JavaScript

Last Updated : 21 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

A palindrome is a word, phrase, number, or other sequence of characters that reads the same forward and backward (ignoring spaces, punctuation, and capitalization). For example, words like "madam" and "racecar" are palindromes.

How to Check if a String is a Palindrome in JavaScript?

To check if a string is a palindrome in JavaScript, we need to:

  • Normalize the string: Convert it to lowercase and remove any non-alphanumeric characters like spaces and punctuation.
  • Reverse the string: Reverse the string to compare it with the original.
  • Compare the original and reversed string: If they are the same, it’s a palindrome.

Approaches to Check for Palindrome in JavaScript

Here are the some common approaches to check for palindrome in JavaScript:

1. Using for loop (Efficient Approach)

In this approach, we use a for loop to iterate through the string from both ends, comparing characters simultaneously. This method is efficient because it can stop early if any mismatched characters are found.

Steps:

  • Loop from the start and end of the string simultaneously.
  • Compare the characters at each index.
  • If any pair of characters do not match, return false.
  • If all characters match, return true.
JavaScript
function isPalindrome(str) {
    let j = str.length - 1
    for (let i = 0; i < str.length / 2; i++) {
        if (str[i] != str[j]) {
            return false;
        }
        j--;
    }
    return true;
}

let str1 = "racecar";
let str2 = "nitin";
let str3 = "Rama";

console.log(isPalindrome(str1));
console.log(isPalindrome(str2));
console.log(isPalindrome(str3));

Output
true
true
false

2. Checking the String from Last

This approach manually reverses the string by iterating from the end to the start, and then compares the reversed string with the original one. If both are identical, the string is a palindrome.

Steps:

  • Initialize a variable to hold the reversed string.
  • Loop through the original string from the end to the beginning and build the reversed string.
  • Compare the original string with the reversed string.
  • Return true if both strings are equal; otherwise, return false.
JavaScript
function isPalindrome(str) {
    let rev = "";
    for (let i = str.length - 1; i >= 0; i--) {
        rev += str[i];
    }
    if (rev == str) {
        return true
    } else {
        return false;
    }
}

let str1 = "racecar";
let str2 = "nitin";
let str3 = "Rama";

console.log(isPalindrome(str1));
console.log(isPalindrome(str2));
console.log(isPalindrome(str3));

Output
true
true
false

3. Using split(), reverse() and join() Methods

JavaScript provides built-in methods that can simplify the palindrome check. We can use split(), reverse(), and join() methods to reverse a string and compare it with the original string in just a few lines of code

  • Use split() to split the string into an array of characters.
  • Use reverse() to reverse the array.
  • Use join() to join the reversed array back into a string.
  • Compare the original string with the reversed string.
  • Return true if both strings are equal; otherwise, return false.
JavaScript
function isPalindrome(str) {
    let rev = str.split("").reverse().join("");

    if (rev == str) {
        return true
    }
    return false

}

let str1 = "racecar";
let str2 = "nitin";
let str3 = "Rama";

console.log(isPalindrome(str1));
console.log(isPalindrome(str2));
console.log(isPalindrome(str3));

Output
true
true
false

Use Cases for Palindromes

Palindromes are not just for fun with words! They have practical applications in programming, such as:

  • Palindrome Strings: Checking if a word or phrase is a palindrome is often part of interview questions or puzzle-solving challenges.
  • Palindrome Numbers: They are sometimes used in algorithms, such as detecting symmetric numbers in certain computational problems.
  • Data Validation: Some data validation tasks may involve ensuring that input values are symmetric, such as in DNA sequence matching or other symmetrical data checks.

Conclusion

Checking for palindromes in JavaScript is straightforward and involves basic string manipulation. By normalizing the string and comparing it with its reverse, we can easily determine if a word, sentence, or number is a palindrome. This concept is useful in various coding challenges, problem-solving exercises, and real-world applications.

To know more about How to check whether a passed string is palindrome or not in JavaScript?


Next Article
Practice Tags :

Similar Reads