Hackerss.com

Hackerss.com is a community of amazing hackers

Hackerss is a community for developers, data scientitst, ethical hackers, hardware enthusiasts or any person that want to learn / share their knowledge of any aspect of digital technology.

Create account Log in
hackerss
hackerss

Posted on

Check if two strings are anagram in Javascript

Check if two strings are anagram - Javascript:


//function to check if two strings are anagram
function isAnagram(str1, str2) {
  // convert both strings to lowercase
  str1 = str1.toLowerCase();
  str2 = str2.toLowerCase();

  // create an array of characters from the first string
  const arr1 = str1.split("");

  // create an array of characters from the second string
  const arr2 = str2.split("");

  // sort the arrays
  arr1.sort();
  arr2.sort();

  // compare the arrays
  if (arr1.toString() === arr2.toString()) {
    return true;
  } else {
    return false;
  }
}

//example
const result = isAnagram("listen", "silent");

//output
console.log(result); //true
Enter fullscreen mode Exit fullscreen mode

Top comments (0)