What is the output of the following code involving WeakRef? let obj = { name: ‘target’ }; const ref = new WeakRef(obj); obj = null; // GC may or may not have run console.log(ref.deref()?.name);

JavaScript Professional Hard

JavaScript Professional — Hard

What is the output of the following code involving WeakRef? let obj = { name: ‘target’ }; const ref = new WeakRef(obj); obj = null; // GC may or may not have run console.log(ref.deref()?.name);

Key points

  • WeakRef allows objects to be garbage collected when no longer needed
  • deref() returns the object if it hasn't been collected
  • Setting obj to null does not guarantee immediate garbage collection

Ready to go further?

Related questions