Understanding Map and Set: ES6 Basics

Understanding Map and Set in ES6

Patrick Karsh
4 min readSep 30, 2023

JavaScript ES6 brings several new features and enhancements, including two new data structures: Map and Set. These structures are designed to enhance the way developers can store and manage data, allowing for more flexibility and efficiency.

Map in ES6

A Map in JavaScript is a collection of key-value pairs where any value, whether an object or a primitive, can be used as either a key or a value. Maps are particularly useful when there is a need to maintain the insertion order of the objects, which is not guaranteed in regular JavaScript objects.

Creating and Manipulating Maps

To create a Map, you instantiate a new Map object and use the set method to add key-value pairs:

let myMap = new Map();
myMap.set('key1', 'value1');
myMap.set('key2', 'value2');

You can retrieve values from a Map using the get method and check for the existence of a key using the has method:

console.log(myMap.get('key1')); // Output: value1
console.log(myMap.has('key2')); // Output: true

Maps are iterable, and you can loop through them using methods like forEach:

myMap.forEach((value, key) => {
console.log(key, value);
});

To remove key-value pairs, you can use the delete method, and to empty a Map, you can use the clear method:

myMap.delete('key1');
myMap.clear();

Set in ES6

A Set in JavaScript is a collection of values where each value must be unique. It means the same value cannot occur more than once in a Set. The value can be either an object or a primitive.

Creating and Manipulating Sets

To create a Set, you instantiate a new Set object and use the add method to add values:

let mySet = new Set();
mySet.add('value1');
mySet.add('value2');
mySet.add('value1'); // Won't be added, as it is a duplicate

You can check for the existence of a value using the has method and loop through a Set using methods like forEach:

console.log(mySet.has('value1')); // Output: true
mySet.forEach(value => {
console.log(value);
});

To remove a value from a Set, you can use the delete method, and to empty a Set, you can use the clear method:

mySet.delete('value1');
mySet.clear();

Differences between Map and Set

The primary difference between a Map and a Set is the way they store data. A Map holds key-value pairs allowing more structured storage, whereas a Set only stores unique values, making it suitable for cases where the collection must maintain uniqueness, such as a list of user IDs.

Comparing Map with Object

While Maps and regular JavaScript objects seem similar as they both hold key-value pairs, there are crucial differences. The keys of an object are Strings and Symbols, but a Map accepts any data type as a key, including functions, objects, and any primitive. Additionally, Maps maintain the insertion order of the elements, which is not the case with objects.

Comparing Set with Array

Sets and Arrays are both collections of values, but Sets only store unique values, eliminating any duplicates. When it comes to checking the existence of a value, Sets are more performant than Arrays, making them preferable when the existence check is crucial for the performance of the application.

Conclusion

The introduction of Map and Set in ES6 has provided developers with more options for storing and managing data. The Map object, with its ability to hold key-value pairs and maintain insertion order, offers a more powerful and flexible alternative to regular objects, especially when the keys are diverse data types. On the other hand, the Set object, with its unique value storage and high-performance existence check, is a valuable tool when dealing with collections that require uniqueness and performance.

Understanding and leveraging these new data structures allow developers to write more efficient, readable, and maintainable code, thereby enhancing the overall quality of JavaScript applications. Whether it’s managing structured data with varying key types using Maps or maintaining a collection of unique values using Sets, ES6 has enriched the JavaScript ecosystem with more capabilities to deal with diverse and complex data management needs.

While this article provides an overview and basic usage of Map and Set, there are more advanced methods and properties available on these objects, and exploring them further would be beneficial for anyone looking to deepen their understanding of JavaScript and improve their coding skills.

--

--

Patrick Karsh
Patrick Karsh

Written by Patrick Karsh

NYC-based Ruby on Rails and Javascript Engineer leveraging AI to explore Engineering. https://linktr.ee/patrickkarsh

No responses yet