Skip to content
Devtoolry

Strings · beginner

JavaScript Program to Check if Two Strings are Anagrams

Check whether two strings are anagrams in JavaScript by sorting their letters, or by counting letters in a Map, ignoring case, spaces and punctuation.

Two words are anagrams when they use the same letters the same number of times: "listen" and "silent", or "Dormitory" and "dirty room". Sorting the letters of both gives the same string exactly when they are anagrams.

Phrases usually ignore spaces and punctuation, so the program normalizes both strings before comparing.

The program

JavaScript
const normalize = (text) =>
  text.toLowerCase().replace(/[^a-z]/g, "").split("").sort().join("");

function isAnagram(a, b) {
  return normalize(a) === normalize(b);
}

console.log(isAnagram("listen", "silent"));
console.log(isAnagram("Dormitory", "dirty room"));
console.log(isAnagram("hello", "world"));
Output
true
true
false

How it works

  1. toLowerCase() and replace(/[^a-z]/g, "") remove case differences, spaces and punctuation.
  2. split("") and sort() put the letters in alphabetical order: "listen" and "silent" both become "eilnst" after join("").
  3. Two anagrams produce the same sorted string, so a simple === comparison decides.

Watch it run line by line

For the second call, two normalize frames run one after the other, and their return rows both show "dimoorrty".

“Visualize step by step” opens the program in the JavaScript Compiler and records every step in your browser. Use the arrow keys to move forwards and backwards; changed variables are highlighted.

Another way: Count letters with a Map

Counting avoids sorting, which matters for very long strings. Add one for each letter of the first string, subtract one for each letter of the second, and check that every count ends at zero.

JavaScript
function isAnagram(a, b) {
  const counts = new Map();
  for (const ch of a.toLowerCase().replace(/\s/g, "")) counts.set(ch, (counts.get(ch) ?? 0) + 1);
  for (const ch of b.toLowerCase().replace(/\s/g, "")) counts.set(ch, (counts.get(ch) ?? 0) - 1);
  return [...counts.values()].every((n) => n === 0);
}

console.log(isAnagram("Astronomer", "Moon starer"), isAnagram("aab", "abb"));
Output
true false

Common mistakes

  • Comparing new Set(a) with new Set(b): sets ignore how often a letter occurs, so "aab" and "abb" would look like anagrams (and two Sets are never === anyway).
  • Calling sort() on a string: it is an array method, so split the string first.
  • Forgetting to remove spaces when comparing phrases.

Try it yourself

  • Group a list of words into anagram groups using the sorted letters as a Map key.
  • Make the check work for accented letters with normalize("NFD").
  • Find all the anagrams of "listen" in a list of words.

Open the program in the JavaScript Compiler, change it and press Run: there is nothing to install, and your code never leaves your browser.

Browse all 25 JavaScript examples, or write your own program in the online JavaScript Compiler.