Check if Two Strings are Anagrams in JavaScript

💻 JavaScript Intermediate – Check if Two Strings are Anagrams An anagram is when two words contain the same letters in a different order. 📌 Problem: Check if two strings are anagrams of each other. function isAnagram(str1, str2) { let a = str1.split("").sort().join(""); let b = str2.split("").sort().join(""); return a === b; } console.log(isAnagram("listen", "silent")); 📤 Output: true 📖 Explanation: • Use split("") to convert strings into arrays. • sort() arranges letters in alphabetical order. • join("") converts arrays back to strings. • If the sorted strings are equal, they are anagrams. 💡 Tip: This method works for case-sensitive anagrams; you can use .toLowerCase() for case-insensitive checks. #JavaScript #Coding #WebDevelopment #FrontendDevelopment #LearnToCode #ProgrammingTips

  • text

To view or add a comment, sign in

Explore content categories