A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/javascript/javascript-map-set-method/ below:

JavaScript Map set() Method - GeeksforGeeks

JavaScript Map set() Method

Last Updated : 23 Jul, 2025

The JavaScript map.set() method is used to add key-value pairs to a Map object. It can also be used to update the value of an existing key. Each value must have a unique key so that they get mapped correctly.

Syntax:
GFGmap.set(key, value);
Parameters:

This method accepts two parameters as mentioned above and described below:

Example 1:

JavaScript
let GFGMap = new Map();

// Adding a key value pair
GFGMap.set(1, "India");

// Getting the value by key
console.log(GFGMap.get(1));

// Updating the existing value
GFGMap.set(1, "England");

// Getting the value back
console.log(GFGMap.get(1));

Output:

India
England

Example 2: Using set() with chaining

JavaScript
let myMap = new Map();

// Adding key value pair with chaining
myMap.set(1, "Rohan").set(2, "Sohan").set(3, "Trojan");

// Getting the values back
console.log(myMap.get(1));
console.log(myMap.get(2));
console.log(myMap.get(3));

Output:

Rohan
Sohan
Trojan
Supported Browsers:

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4