If you’ve ever sat down to build something in JavaScript—maybe a little to-do list app, maybe an entire analytics dashboard—you’ve probably needed a way to store and retrieve data quickly. And not just in a “loop through an array and hope it’s there” kind of way. You wanted something faster, cleaner, and more predictable.
That’s where the idea of a HashMap comes in. In other languages like Java or C#, a
HashMap is a native data structure designed for lightning-fast lookups using a unique key.
JavaScript doesn’t come with a literal HashMap
type out of the box, but it does have
two powerful tools that work in the same spirit: objects and the ES6 Map
object.
The difference between using a plain object and using a Map might seem small at first—kind of like the difference between storing notes in a messy drawer versus using a labeled filing cabinet. But once you understand the subtle distinctions, it’s hard to go back.
A HashMap is like a phonebook in your code. You have a name (key), you have a phone number (value), and you want to jump straight to the number without flipping through every page. That’s exactly what a HashMap does—turns the key into a quick address where the value is stored.
In JavaScript, we mimic this behavior with two main structures: objects and Maps. You can think of
the Map
object as a supercharged version of an object. It doesn’t come with inherited
methods from Object.prototype
, so you don’t risk accidentally overwriting built-in
properties. It also remembers the order in which you add entries—something plain objects only
started doing reliably in modern engines.
// Using a Map as a HashMap
const userPreferences = new Map();
userPreferences.set('theme', 'dark');
userPreferences.set('notifications', true);
console.log(userPreferences.get('theme')); // "dark"
When ECMAScript 2015 (ES6) arrived, it introduced the Map
object.
This was JavaScript’s
first real answer to developers who needed a true key-value store that could handle more than just
strings as keys.
With Map
, you can even use objects or DOM elements as keys without converting them to
strings.
const elementMap = new Map();
const button = document.querySelector('#submit');
elementMap.set(button, { clicked: false });
console.log(elementMap.get(button)); // { clicked: false }
Creating an empty Map is straightforward:
const myMap = new Map();
You can also initialize a Map with data:
const myMap = new Map([
['name', 'Alice'],
['age', 30],
['role', 'Developer']
]);
Adding or updating entries in a Map uses .set(key, value)
:
const settings = new Map();
settings.set('language', 'English');
settings.set('language', 'Spanish'); // updates existing key
Retrieving values uses .get(key)
:
console.log(settings.get('language')); // "Spanish"
Check if a key exists using .has(key)
:
if (settings.has('language')) {
console.log('Language setting found.');
}
Remove a single entry with .delete(key)
:
settings.delete('language');
Clear the entire Map with .clear()
:
settings.clear();
Maps are iterable, so you can loop through them directly.
// Using forEach
settings.forEach((value, key) => {
console.log(`${key}: ${value}`);
});
// Using for...of with entries
for (let [key, value] of settings.entries()) {
console.log(key, value);
}
Objects work well for fixed string keys and lightweight storage, especially for JSON. Maps are better when keys can be any type, when insertion order matters, or when performance is important for frequent changes.
In large applications, replacing objects with Maps for certain workloads can lead to significant performance improvements.
Using Maps as HashMaps in JavaScript can make your code more efficient and easier to manage. The key is knowing when to choose a Map over an object. For dynamic keys, frequent updates, and guaranteed order, Maps are the clear winner. For fixed configurations or JSON structures, objects still hold their ground.
Once you start working with Maps, you might find yourself wondering how you managed without them.
If you enjoyed this article, check out our latest post on Nginx RTMP guide. As always, if you have any questions or comments, feel free to contact us.