 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Are the strings anagrams in JavaScript
Anagrams −
Two strings are said to be anagrams of each other if by rearranging, rephrasing or shuffling the first we can form a string identical to the second.
For example −
'something' and 'emosghtin' are anagrams of each other.
We are required to write a JavaScript function that takes in two string, say str1 and str2 and return true if they are anagrams of each other, false otherwise.
Example
The code for this will be −
const str1 = "something"; const str2 = "emosghtin"; const validAnagram = (str1 = '',str2 = '') => {    let obj1 = {}    let obj2 = {}    if (str1.length !== str2.length){       return false    };    for(let char of str1){       obj1[char]= (obj1[char] || 0) + 1    };    for(let char of str2){       obj2[char]= (obj2[char] || 0) + 1    };    for(let val in obj1){       if(!(val in obj2) || (obj2[val] !== obj1[val])){          return false       }    };    return true; }; console.log(validAnagram(str1, str2));  Output
And the output in the console will be −
true
Advertisements
 