How to Check Whether Two Strings Are Anagrams using JavaScript?

Forums JavaScriptHow to Check Whether Two Strings Are Anagrams using JavaScript?
Staff asked 2 years ago

Answers (1)

Add Answer
Staff answered 2 years ago
  • you can take 2 string and pass below function than result is give if your string is Anagrams or not.
var x = "thecodehubs";
var y = "codehubsthe";
function checkStringAnagram(x,y)
{
   let firstStrLen = x.length;
   let secondStrLen = y.length;
   if(firstStrLen !== secondStrLen)
   {
     console.log('Invalid Input');
     return
   }
   let str1 = x.split('').sort().join('');
   let str2 = y.split('').sort().join('');
   if(str === str2)
   {
     console.log("true");
   }
   else
   {
     console.log("false");
   }
}
checkStringAnagram(x,y)

 

Output:

true

 

Subscribe

Select Categories