This page was translated from English by the community.Learn more and join the MDN Web Docs community.
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에서 명시된 키와 값을 추가하거나 갱신합니다.
In this article
시도해 보기
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)매개변수
keyMap객체에 추가되는 요소의 키. 이 키는 모든 종류의JavaScript 유형(모든원시형 혹은 모든Javascript 객체)가 될 수 있습니다.valueMap객체에 추가되는 요소의 값. 이 값은 모든 종류의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> |