softare development

Check if Number, Word or Phrase is a Palindrome in JavaScript

What is a Palindrome?

A palindrome is any word, phrase, number, or sequence that reads the exact same way forwards and backwards. Think of it like a mirror for text!

ABA Example

Step 1: What is “Aba”?

  • “Aba” is a real city in Nigeria (in Abia State).
  • Let’s write it out: A – B – A

Step 2: Read it FORWARDS:

  A → B → A  
  = "Aba"

Step 3: Read it BACKWARDS:

  A ← B ← A  
  = "Aba" (SAME THING!)

Step 4: Why it works:

  • First letter: A
  • Middle letter: B
  • Last letter: A
    👉 Start and end match perfectly!

Become a JavaScript Master

Another Example is “madam”

“madam” → Reverse = “madam” (Same!)

We can write a Javascript program to check if a given word is a palindrome or not.

function isPalindrome(word) {
  // 1. Convert to lowercase (so "Madam" = "madam")
  const cleanWord = word.toLowerCase();
  
  // 2. Split into letters → ["m", "a", "d", "a", "m"]
  const letters = cleanWord.split('');
  
  // 3. Reverse letters → ["m", "a", "d", "a", "m"] becomes ["m", "a", "d", "a", "m"] backwards? Wait, it's the same!
  const reversedLetters = letters.reverse();
  
  // 4. Join reversed letters → "madam"
  const reversedWord = reversedLetters.join('');
  
  // 5. Compare: Original vs Reversed
  return cleanWord === reversedWord;
}

Result:

console.log(isPalindrome("Ada"));    // true! (Because "ada" reversed = "ada")
console.log(isPalindrome("Aba"));    // true! (City in Nigeria)
console.log(isPalindrome("Hannah")); // true!

console.log(isPalindrome("Lagos"));  // false ("sogaL" ≠ "Lagos")
console.log(isPalindrome("Abuja"));  // false ("ajubA" ≠ "Abuja")

We can do an advance check for sentences and punctuations:

function isAdvancedPalindrome(text) {
  // Remove spaces/punctuation, keep only letters/numbers
  const cleanText = text.toLowerCase().replace(/[^a-z0-9]/g, '');
  
  // Reverse clean text
  const reversedText = cleanText.split('').reverse().join('');
  
  return cleanText === reversedText;
}

// Test with phrases:
console.log(isAdvancedPalindrome("Was it a car or a cat I saw")); // true!
console.log(isAdvancedPalindrome("No lemon, no melon")); // true!

Recent Posts

UI/UX Design — Explained Like You’re 5

Download the Codeflare iOS app and learn on the Go 1. What UI and UX…

35 minutes ago

Costly Linux Mistakes Beginners Make

1. Running Everything as Root One of the biggest beginner errors. Many new users log…

4 weeks ago

How Keyloggers Work

A keylogger is a type of surveillance software or hardware that records every keystroke made…

1 month ago

JavaScript Memoization

In JavaScript, it’s commonly used for: Recursive functions (like Fibonacci) Heavy calculations Repeated API/data processing…

2 months ago

CSS Container Queries: Responsive Design That Actually Makes Sense

For years, responsive design has depended almost entirely on media queries. We ask questions like: “If…

2 months ago

Cron Jobs & Task Scheduling

1. What is Task Scheduling? Task scheduling is the process of automatically running commands, scripts,…

2 months ago