← All tutorials

GoCareerGo Tutorials

JavaScript Tutorial

Variables to closures, promises, the DOM and OOP — a complete JS reference.

Map.prototype.set()

Adds a new key-value pair to a Map, or updates the value if the key already exists. Returns the Map itself, so calls can be chained.

const scores = new Map();
scores.set('Alice', 90).set('Bob', 85);
scores.get('Alice'); // 90
  • Unlike plain objects, Map keys can be ANY type — objects, functions, even NaN
  • Map preserves insertion order when iterated
  • Use Map over Object when keys aren't simple strings, or when you need reliable size/order