Favorites

JavaScript WeakRef Object

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
WeakRef
JavaScript Memory Management · ConstructorThis object is supported by all modern browsers.
Updated 28th November 2026

A WeakRef holds a weak reference to an object, which doesn't stop it from being garbage-collected. It is an advanced tool for caches; most code never needs it.

Description

A WeakRef object holds a weak reference to another object, its target. Unlike a normal reference, it doesn't keep the target alive: if nothing else refers to it, the garbage collector may remove it, after which deref() returns undefined.

When and whether garbage collection happens is unpredictable and differs between browsers, so code must never depend on it. WeakRefs are mainly useful for caches of large objects that can be recreated if they disappear. For associating data with objects, WeakMap is simpler and usually better.

Calling deref() returns the target if it still exists, or undefined if it has been collected, so always check the result before using it. Within a single synchronous piece of code, a target that was alive at the start stays alive until it finishes. The JavaScript specification deliberately leaves the details of garbage collection to each engine, so code that works in one browser may behave differently in another.

Creating

new WeakRef(target)

Example

let data = { big: new Array(1000).fill('x') };
const ref = new WeakRef(data);

console.log(ref.deref() === data);
data = null;   // the object may now be collected at some point
console.log(typeof ref.deref());

Browser Support

The following information will show you the current browser support for the WeakRef object. Hover over a browser icon to see the version that first introduced support for this object.

This object is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee