Mastering ES6 - Sets and Maps

easewithtuts

V Sai Harsha

Posted on September 8, 2023

Mastering ES6 - Sets and Maps

Introduction

In the ever-evolving world of JavaScript, ECMAScript 6 (ES6) introduced a plethora of new features and data structures to make your code more efficient and expressive. Among these additions are Sets and Maps, which bring new ways to handle data in your applications. In this article, we'll delve into Sets and Maps in ES6, exploring their features and practical use cases.

Understanding Sets

A Set is a collection of unique values, which means that no two elements in a Set can be the same. Sets offer a way to manage distinct values efficiently.

Creating a Set

You can create a Set in ES6 using the Set constructor:

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

Adding and Deleting Elements

Sets provide methods to add and delete elements:

mySet.add('apple');
mySet.add('banana');
mySet.add('apple'); // Duplicate, won't be added

mySet.delete('banana'); // Removes 'banana'
Enter fullscreen mode Exit fullscreen mode

Iterating Through a Set

You can iterate through a Set using for...of loops or the forEach method:

for (const item of mySet) {
  console.log(item);
}

mySet.forEach((value) => {
  console.log(value);
});
Enter fullscreen mode Exit fullscreen mode

Checking for Existence

You can check if an element exists in a Set:

console.log(mySet.has('apple')); // true
console.log(mySet.has('grape')); // false
Enter fullscreen mode Exit fullscreen mode

Set Size

You can get the number of elements in a Set using the size property:

console.log(mySet.size); // 1
Enter fullscreen mode Exit fullscreen mode

Sets are useful when you need to store and manage a collection of distinct values efficiently. Common use cases include managing lists of unique items, such as tags or user IDs.

Understanding Maps

Maps are key-value data structures that allow you to store pairs of keys and their associated values. Unlike Objects, Maps can use any data type as keys and maintain the order of key-value pairs.

Creating a Map

You can create a Map using the Map constructor:

const myMap = new Map();
Enter fullscreen mode Exit fullscreen mode

Setting and Getting Values

Maps provide methods to set and retrieve values:

myMap.set('name', 'Alice');
myMap.set('age', 30);

console.log(myMap.get('name')); // 'Alice'
Enter fullscreen mode Exit fullscreen mode

Checking for Key Existence

You can check if a key exists in a Map:

console.log(myMap.has('name')); // true
console.log(myMap.has('city')); // false
Enter fullscreen mode Exit fullscreen mode

Deleting Entries

You can remove entries from a Map:

myMap.delete('age');
Enter fullscreen mode Exit fullscreen mode

Iterating Through a Map

Maps support various iteration methods:

for (const [key, value] of myMap) {
  console.log(`${key}: ${value}`);
}

myMap.forEach((value, key) => {
  console.log(`${key}: ${value}`);
});
Enter fullscreen mode Exit fullscreen mode

Map Size

You can get the number of key-value pairs in a Map using the size property:

console.log(myMap.size); // 1
Enter fullscreen mode Exit fullscreen mode

Maps are especially handy when you need to store key-value pairs, and you want to preserve the order of insertion. They're commonly used for tasks like caching, data retrieval, and managing settings.

Conclusion

Sets and Maps in ES6 bring powerful data structures to JavaScript, offering enhanced capabilities for managing collections of data. Sets excel at storing unique values efficiently, while Maps provide a flexible key-value store with ordered entries. Incorporating Sets and Maps into your JavaScript code can lead to more elegant and efficient solutions for various programming challenges. Experiment with these data structures to harness their full potential in your projects and become a more proficient JavaScript developer.

💖 💪 🙅 🚩
easewithtuts
V Sai Harsha

Posted on September 8, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related