JavaScript – Create a Map

To create a Map in JavaScript, use Map() constructor. Map() constructor accepts an array of arrays, where inner array contains key and value as elements.

Syntax

The syntax to create a Map using Map() constructor is

new Map() //empty map
new Map([[key, value], [key, value]]) //map from array
ADVERTISEMENT

Examples

In the following example, we create an empty map, and insert some key-value pairs into it.

index.html

In the following example, we create a map with key-value pairs derived from an array of arrays passed as argument to Map() constructor.

index.html

Conclusion

In this JavaScript Tutorial, we learned how to create a Map in JavaScript using Map() constructor, with examples.