JavaScript – Insert Key-Value Pair in Map

To insert a new key-value pair to a Map in JavaScript, call set() method on this Map and pass the key and value as arguments.

Syntax

The syntax to insert a new key-value pair into a Map map is

map.set(key, value)
ADVERTISEMENT

Examples

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

index.html

If the key already exists, then the value is updated with the new value.

index.html

Conclusion

In this JavaScript Tutorial, we learned how to insert a new key-value pair into a Map in JavaScript using set() method, with examples.