Tuesday, January 7, 2025
Google search engine
HomeLanguagesJavascriptPalindrome in JavaScript

Palindrome in JavaScript

We will understand how to check whether a given value is a palindrome or not in JavaScript. A palindrome is a value that reads the same from backward or forward. To perform this check we will use the following approaches:

Approach 1: 

  • Iterate over the string from forward and backward direction
  • Check if the character match
  • Return false for the first character that does not match 
  • Return true if all the comparisons are performed and the characters match

Example: This example implements the above approach.

Javascript




function isPalindrome(str) {
    var j = str.length-1
    for(var i=0; i<str.length/2; i++){
        if(str[i]!=str[j]){
            return false;
        }
        j--;
    }
    return true;
}
  
var str1 = "racecar";
var str2 = "nitin";
var str3 = "Rama";
  
console.log(isPalindrome(str1));
console.log(isPalindrome(str2));
console.log(isPalindrome(str3));


Output:

true
true
false

Approach 2:

  • Initialize a variable and store the reverse of the value in it using for loop
  • Compare the original value with the reversed value
  • If both values are equal return true else return false

Example: This example implements the above approach.

Javascript




function isPalindrome(str) {
    var rev="";
    for(var i=str.length-1; i>=0; i--){
        rev+= str[i];
    }
    if(rev==str){
        return true
    } else{
        return false;
    }
}
  
var str1 = "racecar";
var str2 = "nitin";
var str3 = "Rama";
  
console.log(isPalindrome(str1));
console.log(isPalindrome(str2));
console.log(isPalindrome(str3));


Output:

true
true
false

Approach 3: 

  • Use inbuilt string methods like split() to split the string into characters array
  • Reverse the array using reverse() method
  • Join the array into a string using join() method
  • Store this value inside another variable
  • Compare the values and return true if both are equal

Example: This example implements the above approach on string

Javascript




function isPalindrome(str) {
    var rev=str.split("").reverse().join("");
  
    if(rev == str){
        return true
    }
    return false
                 
}
  
var str1 = "racecar";
var str2 = "nitin";
var str3 = "Rama";
  
console.log(isPalindrome(str1));
console.log(isPalindrome(str2));
console.log(isPalindrome(str3));


Output:

true
true
false

Example: This example implements the above approach to Number

Javascript




function isPalindrome(num) {
    var str = num.toString();
    var rev=str.split("").reverse().join("");
  
    if(rev == str){
        return true
    }
    return false
                 
}
  
var str1 = 1234321;
var str2 = 112211;
var str3 = 12345;
  
console.log(isPalindrome(str1));
console.log(isPalindrome(str2));
console.log(isPalindrome(str3));


Output: The Number is first converted to a string and then compared using the previous approach

true
true
false

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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments