Navigate back to the homepage

How to check if two strings are anagrams of each other in Javascript?

Infinitbility
JavaScript
May 28th, 2022 · 1 min read

Hi Friends 👋,

Welcome To Infinitbility! ❤️

Today, I’m going to show How do I check if two strings are anagrams of each other in Javascript, here I will write a custom function to check if two strings are anagrams of each other or not.

What is an anagram?

An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. For example, the word anagram itself can be rearranged into nag a ram, also the word binary into brainy, and the word adobe into the abode.

Let’s start the today’s tutorial How do you check if two strings are anagrams of each other in Javascript?

Let’s understand how we can do

In the given strings ‘a’ and ‘b’, we will check if they are of the same length and then we will sort the strings. If both the strings are equal, then return “True”; if not, then print “False”.

  • Take Input two strings ‘a’ and ‘b’
  • A function checkAnagramStrings(string a, string b) which will return true if they are an anagram of each other otherwise false.
  • Find the length of both strings and check if they are the same.
  • Now sort both strings in lexicographical order and check if they are equal or not.
  • Return true or false accordingly.
1// create checkAnagramStrings() which accept two parameter
2function checkAnagramStrings(a, b) {
3 // check length of both string
4 if(a.length !== b.length){
5 return false;
6 }
7
8 // make array from string, sort the array, and make string
9 let str1 = a.split('').sort().join('');
10 let str2 = b.split('').sort().join('');
11
12 // compare both string
13 if(str1 === str2){
14 return true;
15 } else {
16 return false;
17 }
18}
19
20console.log(checkAnagramStrings("infinit", "tinifni")); // true
21console.log(checkAnagramStrings("infinit", "bility")); // false

To validate the above program we have tried both valid cases and invalid cases, let’s check the output.

javascript, check if two strings are anagrams of each other example
javascript, check if two strings are anagrams of each other example

I hope it’s help you, All the best 👍.

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

How to check if a string is equal to another string in javascript?

Example of check if a string is equal to another string in javascript with step by step guide.

May 27th, 2022 · 1 min read

How to check if a string is hexadecimal in javascript?

Example of check if a string is hexadecimal in javascript with step by step guide.

May 27th, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]