WeakMap.prototype.set() — JS Technical Interview Questions & Answers

Adds a key-value pair with an object key.

WeakMap.prototype.set()

  • WeakMap.prototype.set() is a JS topic. In plain words you use it for fail students only in a marks list. Don’t start with a slogan — start with that picture.
  • Smallest example: filter(m => m < 40). Type it, run it, and say what you see. If you can do that from memory, you know WeakMap.prototype.set().
  • From the example next to this theory: Adds a key-value pair with an object key.
  • Trap: mutating while filtering. Fix that before you talk about advanced JS.
  • Viva: what is WeakMap.prototype.set()? Then show filter(m => m < 40). Then name the trap.

Technical Interview Tip: What is WeakMap.prototype.set()? Show this: filter(m => m < 40). Trap: mutating while filtering.

Code Examples for WeakMap.prototype.set()

WeakMap.prototype.set() — 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.prototype.set() — weakMap keys must be objects; entries do not prevent GC of those keys.

How to Explain WeakMap.prototype.set() to an Interviewer

What is WeakMap.prototype.set()?

Used for fail students only a marks list Not just the heading

Give one small example of WeakMap.prototype.set().

filter(m => m < 40) Adds a key-value pair with an object key

What mistake do beginners make with WeakMap.prototype.set()?

mutating while filtering Show the fix

Where do you use WeakMap.prototype.set()?

a marks list fail students only