How to Check Whether Two Strings Are Anagrams using JavaScript?
Answers (1)
Add Answer- 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