JS Maps 🎯

beginner
17 min

JS Maps 🎯

Welcome to our comprehensive guide on JavaScript Maps! In this tutorial, we'll explore this powerful data structure and learn how to use it effectively in your projects.

What is a Map? 📝

A Map is a collection of key-value pairs in JavaScript, similar to an Object, but with some key differences. Unlike Objects, Maps can store any data type as keys (not just strings and symbols), and they maintain the insertion order of their entries.

Creating a Map 💡

To create a Map, use the Map constructor:

javascript
const myMap = new Map();

Let's create a simple Map:

javascript
const myMap = new Map([ ['name', 'John Doe'], ['age', 30], ['city', 'New York'] ]);

Accessing Map Values ✅

To access Map values, use the get() method:

javascript
console.log(myMap.get('name')); // Output: John Doe

Adding and Removing Entries 📝

Add a new entry:

javascript
myMap.set('job', 'Engineer');

Remove an entry:

javascript
myMap.delete('age');

Iterating Over Map Entries 💡

Iterate over Map entries using the for...of loop:

javascript
for (let entry of myMap) { console.log(entry); }

Quiz 💡

Quick Quiz
Question 1 of 1

What is a Map in JavaScript?

Conclusion 🎯

In this lesson, we've covered the basics of JavaScript Maps, including what they are, how to create them, and how to access, add, and remove entries. We've also learned how to iterate over Map entries. With this knowledge, you're well on your way to mastering this powerful data structure!

Stay tuned for more in-depth lessons on JavaScript Maps here at CodeYourCraft. Happy coding! 😊