Palindrome in JavaScript – GeeksforGeeks

Palindrome in JavaScript – GeeksforGeeks

Enhance Article

Save Article

Like Article

Enhance Article

Save Article

Like Article

We are going to perceive test whether or not a given worth is a palindrome or not in JavaScript. A palindrome is a price that reads the identical from backward or ahead. To carry out this test we are going to use the next approaches:

Method 1: 

  • Iterate over the string from ahead and backward path
  • Examine if the character match
  • Return false for the primary character that doesn’t match 
  • Return true if all of the comparisons are carried out and the characters match

Instance: This instance implements the above strategy.

Javascript

perform isPalindrome(str) {

    var j = str.length-1

    for(var i=0; i<str.size/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

Method 2:

  • Initialize a variable and retailer the reverse of the worth in it utilizing for loop
  • Examine the unique worth with the reversed worth
  • If each values are equal return true else return false

Instance: This instance implements the above strategy.

Javascript

perform 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

Method 3: 

  • Use inbuilt string strategies like cut up() to separate the string into characters array
  • Reverse the array utilizing reverse() methodology
  • Be a part of the array right into a string utilizing be a part of() methodology
  • Retailer this worth inside one other variable
  • Examine the values and return true if each are equal

Instance: This instance implements the above strategy on string

Javascript

perform isPalindrome(str) {

    var rev=str.cut up("").reverse().be a part of("");

  

    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

Instance: This instance implements the above strategy to Quantity

Javascript

perform isPalindrome(num) {

    var str = num.toString();

    var rev=str.cut up("").reverse().be a part of("");

  

    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 Quantity is first transformed to a string after which in contrast utilizing the earlier strategy

true
true
false

To know extra about Learn how to test whether or not a handed string is palindrome or not in JavaScript?

Like Article

Save Article