Palindrome Program in JavaScript (5 Ways)
A palindrome is a word, number, phrase, or sequence of characters that reads the same backward as forward. Examples include:
-
Palindrome strings: "madam", "radar", "level".
-
Palindrome numbers: 121, 1331, 12321.
This program tutorial demonstrates how to write a JavaScript program for palindrome checks, covering strings, numbers, and case-insensitive comparisons.
Palindrome Check for a String in JavaScript
This program checks if a given string is a palindrome by reversing it and comparing it to the original.
Code
function isPalindromeString(str) {
const reversed = str.split('').reverse().join('');
return str === reversed;
}
// Test the function
const testString = "madam";
console.log(`Is '${testString}' a palindrome?`, isPalindromeString(testString));
Output
Is 'madam' a palindrome? true
Explanation
-
The split method splits the string into an array of characters.
-
The reverse method reverses the array.
-
The join method converts the array back into a string for comparison.
Palindrome Check for a Number in JavaScript
This program checks if a given number is a palindrome by reversing its digits and comparing it to the original number.
Code
function isPalindromeNumber(num) {
const strNum = num.toString();
const reversed = strNum.split('').reverse().join('');
return strNum === reversed;
}
// Test the function
const testNumber = 121;
console.log(`Is ${testNumber} a palindrome?`, isPalindromeNumber(testNumber));
Output
Is 121 a palindrome? true
Explanation
-
The number is converted to a string using toString().
-
The same reversal logic as the string palindrome check is applied.
JavaScript Program to Check Palindrome Using Built-in Functions
This program demonstrates a concise way to check for palindromes using JavaScript’s built-in methods.
Code
function isPalindromeUsingBuiltIn(input) {
const str = input.toString().toLowerCase();
return str === [...str].reverse().join('');
}
// Test the function
const testInput = "Radar";
console.log(`Is '${testInput}' a palindrome?`, isPalindromeUsingBuiltIn(testInput));
Output
Is 'Radar' a palindrome? true
Explanation
-
The input is converted to a lowercase string to ensure case-insensitive comparison.
-
The spread operator (...) creates an array for reversal and joining.
JavaScript Program to Check Palindrome Using For Loop
This program uses a for loop to check for palindromes by comparing characters from the start and end of the string.
Code
function isPalindromeUsingForLoop(str) {
const len = str.length;
for (let i = 0; i < len / 2; i++) {
if (str[i] !== str[len - 1 - i]) {
return false;
}
}
return true;
}
// Test the function
const loopTestString = "level";
console.log(`Is '${loopTestString}' a palindrome?`, isPalindromeUsingForLoop(loopTestString));
Output
Is 'level' a palindrome? true
Explanation
-
The for loop compares the characters at the start and end of the string, moving towards the center.
-
If a mismatch is found, the function returns false; otherwise, it returns true.
Case-Insensitive Palindrome Check in JavaScript
This program performs a case-insensitive comparison by converting the string to lowercase before checking.
Code
function isCaseInsensitivePalindrome(str) {
const normalized = str.toLowerCase();
const reversed = normalized.split('').reverse().join('');
return normalized === reversed;
}
// Test the function
const caseInsensitiveString = "Level";
console.log(`Is '${caseInsensitiveString}' a palindrome?`, isCaseInsensitivePalindrome(caseInsensitiveString));
Output
Is 'Level' a palindrome? true
Explanation
-
The toLowerCase() method ensures that comparisons are case-insensitive.
-
The rest of the logic is similar to the standard string palindrome check.
Concepts Used in Above Programs
String Methods
-
split(), reverse(), and join() are key methods for reversing strings.
Number to String Conversion
-
The toString() method is used to convert numbers to strings for processing.
Case Normalization
-
The toLowerCase() method ensures consistent comparisons by normalizing the case of characters.
Spread Operator
-
The spread operator (...) provides a shorthand for converting a string to an array.
For Loop
-
Used for character-by-character comparison to check for mismatches.