Checking whether a string is a palindrome in JavaScript means verifying if it reads the same forward and backward. It is a common programming task used to practice string manipulation and logic building.
- Compare string with its reversed version.
- Ignore spaces, punctuation, or case if needed.
- Uses string methods like split(), reverse(), join().
- Helpful for coding practice and interviews.
- Demonstrates basic string handling skills.
Example:
Input: "race"
Output: passed string is not a palindrome
Explanation: if we write "race" in reverse that is "ecar" it not matches with first string so it is not a palindrome.
Input: "hellolleh"
Output: passed string is palindrome.
Below are the methods by which we can check whether a passed string is palindrome or not:
Method 1: Naive Approach
- First, we iterate over a string in forward and backward directions.
- Check if all forward and backward character matches, and return true.
- If all forward and backward character does not matches, return false.
- If a return is true, it is a palindrome.
function check_palindrome(str) {
let j = str.length - 1;
for (let i = 0; i < j / 2; i++) {
let x = str[i];//forward character
let y = str[j - i];//backward character
if (x != y) {
// Return false if string not match
return false;
}
}
// Return true if string is palindrome
return true;
}
// Function that print output if string is palindrome
function is_palindrome(str) {
// Variable that is true if string is palindrome
let ans = check_palindrome(str);
// Condition checking ans is true or not
if (ans == true) {
console.log("passed string is palindrome ");
}
else {
console.log("passed string not a palindrome");
}
}
// Test variable
let test = "racecar";
is_palindrome(test);
Method 2: Reversing the string
Another approach is to reverse a string and check if the initial string matches the reverse string or not.
Follow the following steps :Â
- Initialize reverse_str a variable that stores the reverse of the passed string.
- Compare the string to reverse_str.
- If matches, it is a palindrome.
- The else string is not a palindrome.
function reverse(str) {
// Variable holds reverse string
let rev_str = "";
for (let i = str.length - 1; i >= 0; i--) {
rev_str += str[i];
}
// Return reverse string
return rev_str;
}
//Function checking string is palindrome or not
function is_palindrome(str) {
reverse_str = reverse(str);
// Condition checking if reverse str is
// same as string it is palindrome
// else not a palindrome
if (reverse_str === str) {
console.log("passed string is palindrome ");
}
else {
console.log("passed string is not palindrome")
}
}
let test = "hellolleh";
is_palindrome(test);
Method 3: Using the split(), reverse(), and join() Methods
Another approach, which is through the shortest approach, uses the split(), reverse(), and join() methods.
- Split the string of characters into several different characters (which is though unsorted at the moment).
- Use the reverse() method to reverse all the characters of the string alphabetically.
- Then apply the join() method in order to join all the characters of the string (which are now sorted).
let checkPalindrome = (stringg) => {
return stringg === stringg.split("").reverse().join("");
};
console.log("Is Palindrome? : " + checkPalindrome("noon"));
console.log("Is Palindrome?: " + checkPalindrome("apple"));
Method 4: Using Array.every()
Using Array.every(), the approach iterates over the characters of the string from both ends simultaneously, comparing each pair. If all pairs match, it returns true, indicating the string is a palindrome; otherwise, false.
function isPalindrome(str) {
return str.split('').every((char, index) => char === str[str.length - index - 1]);
}
console.log(isPalindrome("radar"));
console.log(isPalindrome("hello"));
console.log(isPalindrome("level"));
Method 5: Using Two Pointers
This approach uses two pointers, one starting from the beginning of the string and the other from the end, moving towards the center, comparing characters along the way.
function isPalindrome(str) {
let left = 0;
let right = str.length - 1;
while (left < right) {
if (str[left] !== str[right]) {
return false;
}
left++;
right--;
}
return true;
}
console.log(isPalindrome("madam"));
console.log(isPalindrome("test"));
console.log(isPalindrome("abba"));