This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

Map.prototype.set()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨2015년 7월⁩.

Map 인스턴스의 set() 메서드는 이 Map에서 명시된 키와 값을 추가하거나 갱신합니다.

시도해 보기

const map1 = new Map();
map1.set("bar", "foo");

console.log(map1.get("bar"));
// Expected output: "foo"

console.log(map1.get("baz"));
// Expected output: undefined

구문

js
set(key, value)

매개변수

key

Map 객체에 추가되는 요소의 키. 이 키는 모든 종류의 JavaScript 유형(모든 원시형 혹은 모든 Javascript 객체)가 될 수 있습니다.

value

Map 객체에 추가되는 요소의 값. 이 값은 모든 종류의 JavaScript 유형(모든 종류의 원시형 혹은 모든 종류의 Javascript 객체)가 될 수 있습니다.

반환 값

Map 객체.

예제

set() 사용하기

js
const myMap = new Map();

// map에 새로운 요소를 추가합니다
myMap.set("bar", "foo");
myMap.set(1, "foobar");

// map에 요소를 갱신합니다
myMap.set("bar", "baz");

연속으로 set() 사용

set()메서드는 같은 Map 객체를 반환하기 때문에 아래와 같이 연속으로 호출할 수 있습니다.

js
// 연속적으로 새로운 요소를 추가합니다.
myMap.set("bar", "foo").set(1, "foobar").set(2, "baz");

명세서

Specification
ECMAScript® 2026 Language Specification
# sec-map.prototype.set

브라우저 호환성

같이 보기