bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/JavaScript/Working with Data
JavaScript•Working with Data

JavaScript Maps

The Map Object

A JavaScript Map is an object that can store collections of key-value pairs, similar to a dictionary in other programming languages.

Maps differ from standard objects in that keys can be of any data type .

Map Characteristics

  • Key Types Map keys can be any type (strings, numbers, objects, etc).
  • Insertion Order The Map remembers the original insertion order of the keys.
  • Size The number of items in a Map is easily retrieved using the size property.
  • Performance Maps are optimized for frequent additions and removals of key-value pairs.
  • Iteration Maps are iterable, allowing for direct use of for...of loops or methods like forEach().
  • Iteration Order The original order is preserved during iteration.

Maps are similar to both Objects (unique key/value collection) and Arrays (ordered values collection).

But if you look close, Maps are most similar to Objects.

How to Create a Map

You create a JavaScript Map by

  • Create a new Map and add elements with Map.set()
  • Passing an existing Array to the new Map() constructor
Map.set()

Adding Map Values

You can add elements to a Map with the set() method:

fruits.set("mangos", 100);

Changing Map Values

The set() method can also be used to change existing Map values:

fruits.set("apples", 200);

The get() Method

The get() method gets the value of a key in a Map:

fruits.get("apples"); // Returns 500

Maps are Objects

// Returns object:
typeof fruits;

instanceof Map returns true:

// Returns true:
fruits instanceof Map;

JavaScript Objects vs Maps

ObjectMap
Not directly iterableDirectly iterable
Do not have a size propertyHave a size property
Keys must be Strings (or Symbols)Keys can be any datatype
Keys are not well orderedKeys are ordered by insertion
Have default keysDo not have default keys

Learn More

JavaScript Map Methods

JavaScript Weak Maps

JavaScript Map Reference

JavaScript Sets

Browser Support

Map is an ES6 feature .

ES6 is fully supported in all modern browsers since June 2017:

Chrome 51Edge 15Firefox 54Safari 10Opera 38
May 2016Apr 2017Jun 2017Sep 2016Jun 2016

Previous

JavaScript Sets

Next

JavaScript Math Object

This chapter

Overview
37

Lessons

216m

Read time

1. JavaScript Strings2. JavaScript Numbers3. JavaScript Dates4. JavaScript Arrays5. JavaScript Sets6. JavaScript Maps7. JavaScript Math Object8. JavaScript RegExp9. JavaScript String Templates10. JavaScript Number Methods11. JavaScript Date Formats12. JavaScript Array Methods13. JavaScript Set Methods14. JavaScript Map Methods15. JavaScript Random16. JavaScript RegExp Flags17. JavaScript String Methods18. JavaScript Number Properties19. JavaScript Get Date Methods20. JavaScript Array Search21. JavaScript Set Logic22. JavaScript WeakMap23. RegExp Character Classes24. JavaScript String Search25. JavaScript Bitwise Operations26. JavaScript Set Date Methods27. JavaScript Array Sort28. JavaScript WeakSet29. RegExp Meta Characters30. JavaScript BigInt31. JavaScript Array Iterations32. Regular Expression Assertions33. JavaScript Array Const34. RegExp Quantifiers35. JavaScript RegExp Patterns36. JavaScript RegExp Objects37. RegExp Methods

On this page

The Map ObjectMap CharacteristicsHow to Create a MapAdding Map ValuesChanging Map ValuesThe get() MethodMaps are ObjectsJavaScript Objects vs MapsBrowser Support