🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to JavaScript Notes
Topic #74

JS 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.

Note: 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

Examples

// Create an empty Map

const fruits = new Map();

// Set Map Values

fruits.set("apples", 500);

fruits.set("bananas", 300);

fruits.set("oranges", 200);

Adding Map Values

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

Example

fruits.set("mangos", 100);

Changing Map Values

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

Example

fruits.set("apples", 200);

The get() Method

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

Example

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

Maps are Objects

Example

// Returns object:

typeof fruits;

instanceof Map returns true:

Example

// Returns true:

fruits instanceof Map;

JavaScript Objects vs Maps

Differences between JavaScript Objects and Maps:

Object Map
Not directly iterable Directly iterable
Do not have a size property Have a size property
Keys must be Strings (or Symbols) Keys can be any datatype
Keys are not well ordered Keys are ordered by insertion
Have default keys Do not have default keys

Note: 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:

Want to go beyond the notes?

Join CodingNow 2.0's JavaScript course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

JS Maps – FAQs

Quick answers about learning JS Maps in JavaScript.

This free note from CodingNow 2.0 explains JS Maps in JavaScript — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every JavaScript topic on CodingNow 2.0, including JS Maps, is 100% free with no signup required.
With focused practice, most students grasp JS Maps in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now