Map holds key-value pairs where the keys can be any datatype.
A Map remembers the original insertion order of the keys.
How to Create a Map
You can create a JavaScript Map by:
Passing an Array to new Map()
Create a Map and use Map.set()
The new Map() Method
You can create a Map by passing an Array to the new Map() constructor:
Example
// Create a Map
const fruits = new Map([
["apples", 500],
["bananas", 300],
["oranges", 200]
]);
The set() Method
You can add elements to a Map with the set() method:
Example
// Create a Map
const fruits = new Map();
// Set Map Values
fruits.set("apples", 500);
fruits.set("bananas", 300);
fruits.set("oranges", 200);
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
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
Complete Map Reference
For a complete reference, go to our:
Complete JavaScript Map Reference.
The reference contains descriptions and examples of all Map Properties and Methods.
Top comments (0)
Some comments may only be visible to logged-in visitors. Sign in to view all comments.