DEV Community

Cover image for Map & Set: The Underrated Heroes of JavaScript
Kunal
Kunal

Posted on

Map & Set: The Underrated Heroes of JavaScript

While writing the blog on Array method, I have came across some topics that are new to me and Map and Set are one of them.

If you’d like to explore them too πŸ‘‡

So in this blog we will cover :

Topics to Cover

  • Before Map & Set (The Old Way)
  • What Map is
  • Difference between Map and Object
  • What Set is
  • Difference between Set and Array
  • When to use Map and Set

Before Map & Set

Before learning Set and Map , I tried doing things in the old ways using array and objects.

  1. For Map we do
const data = {};

data["name"] = "Kunal";
data["age"] = 21;

console.log(data["name"]); // Kunal
Enter fullscreen mode Exit fullscreen mode
  1. For Set we do

const arr = [1,2,2,3,3,5]
const unique = []

for (let i = 0; i < arr.length; i++) {
   if(!unique.includes(arr[i])){
    unique.push(arr[i])
   }
}
console.log(unique); // [1,2,3,5]
Enter fullscreen mode Exit fullscreen mode

It worked but it required more code and wasn't clean or efficient.
That's when i come across Set and Map.


Map

The Map object stores data in a key/value pair structure just like an object. The main difference between a regular object and a Map are:

  • A Map can have any type of data as key Value
  • A Map maintains the order of the data

How to create a Map object

To create a Map object you can call the Map() constructor as follows :


const newMap = new Map()
Enter fullscreen mode Exit fullscreen mode

Like objects, Map is not just data, it comes with powerful built-in methods and properties.

Map Object Methods and Properties

A Map object has the following methods and properties :

Don’t just read try each method and property yourself.
Here, I’m only covering some important ones of the Map object
.

  1. To insert data into the Map object you can use set() method.
const myMap = new Map()

myMap.set(1 , "like this blog")
myMap.set(2 , "hello world")

console.log(myMap)
Enter fullscreen mode Exit fullscreen mode

The output of the above code

Map(2) { 1 => 'like this blog', 2 => 'hello world' }
Enter fullscreen mode Exit fullscreen mode
  1. To retrieve a value from the Map object you can use the get() method
console.log(myMap.get(1))
Enter fullscreen mode Exit fullscreen mode

You have to pass the key as its argument

  1. To see if a certain key exist in a Map object we can use has() method

myMap.has(1) // true
Enter fullscreen mode Exit fullscreen mode

Now Lets see the main difference between Map and the object


Difference between Map and Object

Feature Object 🧱 Map πŸ—ΊοΈ
Key Type String / Symbol only Any type (object, number, etc.)
Order Not strictly guaranteed* Maintains insertion order
Size No direct property size property
Iteration Not directly iterable Directly iterable
Use Case Simple/static data Dynamic & frequent updates

Set

The Set object allows you to store a collection of element just like an array. The difference between a Set and array are :

  • A Set require all elements to be unique
  • A Set has fewer methods for data manipulation

How to create a Set object

To create a new Set object you need yo call a Set() constructor as follows :

const mySet = new Set()
Enter fullscreen mode Exit fullscreen mode

Like array, Set is not just data, it comes with powerful built-in methods and properties.

Set Object Methods and Properties

Like Map here i only cover some important methods and property rest you can do it your own.

  1. To add an element to the Set object, you can use the add() method:

const mySet = new Set()

mySet.add(1)
mySet.add(2)
mySet.add(3)

console.log(mySet) // Set(3) { 1, 2, 3 }
Enter fullscreen mode Exit fullscreen mode
  1. To check if the Set has a specific value, use the has() method:
mySet.has('Jack'); // true

mySet.has('Michael'); // false
Enter fullscreen mode Exit fullscreen mode
  1. To check the number of Items in Set we use size() method:
console.log(mySet.size) // 3
Enter fullscreen mode Exit fullscreen mode

Let see the Difference between Array and Set


Difference between Set and Array

Feature Array πŸ“š Set πŸ”’
Duplicates Allowed Not allowed (unique only)
Order Maintains order Maintains insertion order
Access By index (arr[0]) No index access
Use Case List of items Unique collection
Methods Focus push(), pop() add(), has()

When to use Map

  • You need key β†’ value relationship
  • Keys are not just strings (can be objects, numbers)
  • You want fast lookup by key
Example: userId β†’ userData
Enter fullscreen mode Exit fullscreen mode

When to use Set

  • You only care about unique values
  • You want to remove duplicates
  • You just need to check existence
Example: unique tags, unique IDs
Enter fullscreen mode Exit fullscreen mode

Thanks for reading ! if enjoyed this blog , you can read more on this πŸ‘‡

Top comments (0)