JavaScript WeakMap Methods

Beginner
4 min read
Updated: Nov 2026
6 method guides
Method index

About This Index

This page is the JavaScript WeakMap Methods index for the tutorial track. Each row links to a full guide with syntax, three runnable examples, browser notes, and FAQs.

Learn what a WeakMap is on /js/weakmap. Use this page when you need a specific API such as set(), get(), or delete().

Keys must be objects.

Every WeakMap method takes an object as the key. set() throws a TypeError for a string or number. get(), has(), and delete() never throw: they answer undefined or false.

How to Call a WeakMap Method

Every method is called with a dot on the WeakMap. There are no static helpers:

JavaScript
const wm = new WeakMap();
const user = { name: "Ann" };

wm.set(user, "admin");     // store a value (returns wm)
wm.get(user);              // "admin"
wm.has(user);              // true
wm.delete(user);           // true

// Newer upsert methods: check support first
wm.getOrInsert(user, "guest");
wm.getOrInsertComputed(user, function (key) { return "guest"; });

JavaScript WeakMap Methods Table

All method guides in this JavaScript tutorial series (6 tutorials), grouped by what they do.

Store and update

3 methods

Attach a value to an object key. set() always writes; the two newer upsert methods write only when the key is missing.

MethodDescriptionTutorial
set()Store a value for an object key. Returns the WeakMap, so calls can be chained.Open
getOrInsert()Read the value for a key, or store a default first when the key is missing (newer method).Open
getOrInsertComputed()Read the value for a key, or call a function to build and store it when the key is missing (newer method).Open

Read

2 methods

Look a key up without changing anything. Neither method ever throws, even for keys that WeakMap cannot hold.

MethodDescriptionTutorial
get()Read the value stored for a key, or undefined when the key is missing. Never throws.Open
has()Check whether a key is stored. Returns true or false and never throws.Open

Remove

1 method

Delete one entry on purpose. You do not need this for memory: dead keys are cleaned up for you.

MethodDescriptionTutorial
delete()Remove one entry by key. Returns true if something was removed, false otherwise.Open

WeakMap Constructor and What Is Missing

A WeakMap is small on purpose. Here is how to create one, and the things it does not have. Compare this with a Map, which has all of them.

MemberWhat it gives youSince
new WeakMap()Create an empty WeakMap.ES2015
new WeakMap([[key, value], ...])Create a WeakMap that already holds some pairs. Every key must be an object or a local symbol.ES2015
weakMap.sizeDoes not exist. A WeakMap cannot tell you how many entries it holds, so you get undefined.Not available
weakMap.clear()Does not exist. To start over, create a new WeakMap.Not available
weakMap.keys(), values(), entries(), forEach()None of these exist. A WeakMap cannot be listed.Not available
for (const x of weakMap)Throws a TypeError. A WeakMap is not iterable.Not available
Symbol("id") as a keyLocal symbols (made with Symbol(), not Symbol.for()) are allowed as keys.ES2023

Frequently Asked Questions

There are four classic methods: set(), get(), has(), and delete(). Two newer ones, getOrInsert() and getOrInsertComputed(), are still arriving in engines. A WeakMap has no size property, no clear(), and no way to loop over its entries.
The WeakMap page explains what weak keys are, why they matter, and how a WeakMap differs from a Map. This index lists every method tutorial, plus the constructor and the members a WeakMap does not have, so you can jump straight to the API you need.
Put a dot after the WeakMap and call the method with an object key: wm.set(obj, 1), wm.get(obj), wm.has(obj), wm.delete(obj). WeakMap has no static helper methods.
getOrInsert() and getOrInsertComputed() are the new ones. They were added to Map and WeakMap together and are not in every engine yet, so test for them before use. set(), get(), has(), and delete() have been in JavaScript since ES2015 and work everywhere.
Read JavaScript WeakMap first, then set() and get(), which you will use every day. After that learn has() and delete(). Finish with getOrInsert() and getOrInsertComputed() if the browsers you target support them.

Start with delete()

Learn how to remove an entry on purpose, and why you rarely have to.

delete() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful