WeakMap Basics — JS Technical Interview Questions & Answers

Map with weakly-held object keys for memory-safe caches.

WeakMap Basics

  • WeakMap Basics (js intro) is a JS topic. In plain words you use it for let total = 0 in a cart script. Don’t start with a slogan — start with that picture.
  • Smallest example: let for change, const for fixed. Type it, run it, and say what you see. If you can do that from memory, you know WeakMap Basics (js intro).
  • From the example next to this theory: Map with weakly-held object keys for memory-safe caches.
  • Trap: var leak / const reassign. Fix that before you talk about advanced JS.
  • Viva: what is WeakMap Basics (js intro)? Then show let for change, const for fixed. Then name the trap.

Technical Interview Tip: What is WeakMap Basics (js intro)? Show this: let for change, const for fixed. Trap: var leak / const reassign.

Code Examples for WeakMap Basics

WeakMap Basics — working example

const secret = new WeakMap();
const user = { id: 1 };
secret.set(user, "token-abc");
console.log(secret.get(user));
console.log(secret.has(user));

WeakMap Basics — weakMap keys must be objects; entries do not prevent GC of those keys.

How to Explain WeakMap Basics to an Interviewer

What is WeakMap Basics (js intro)?

Used for let total = 0 a cart script Not just the heading

Give one small example of WeakMap Basics (js intro).

let for change, const for fixed Map with weakly-held object keys for memory-safe caches

What mistake do beginners make with WeakMap Basics (js intro)?

var leak / const reassign Show the fix

Where do you use WeakMap Basics (js intro)?

a cart script let total = 0