Describe the Issue
The WeakSet example in this lesson has three correctness issues:
- Objects are added to the WeakSet via object literals passed directly to
.add(). Because WeakSet holds only weak references, these objects have no remaining strong reference once the statement completes and are eligible for garbage collection.
- Separately, since no variable is bound to them, there's no way to reference them in any later
.has() call — a literal that looks identical evaluates to a distinct object, and WeakSet compares by reference identity.
- A
.has() call is made with a string argument, which will always return false. WeakSet can only store objects; strings are not valid entries.
Proposed fix: Refactor the example to bind objects to variables before adding them to the WeakSet, and pass one of those variables to .has() instead of a string.
Affected Page
https://www.freecodecamp.org/learn/javascript-v9/lecture-working-with-maps-and-sets/what-are-sets-in-javascript-and-how-does-it-differ-from-weaksets
Your code
const treeWeakSet = new WeakSet();
treeWeakSet.add({ name: 'Baobab' });
treeWeakSet.add({ name: 'Jackalberry' });
treeWeakSet.add({ name: 'Mopane Tree' });
treeWeakSet.add({ name: 'Breadfruit' });
treeWeakSet.delete('Jackalberry');
console.log(treeWeakSet.has('Jackalberry')); // false
console.log(treeWeakSet);
Expected behavior
The lesson should demonstrate a working WeakSet example where objects are bound to variables before being added, and where .has() is called with an object reference. The current example cannot work as shown because objects added via bare literals to .add() have no remaining reference to pass to a later .has(), and the .has() call uses a string, which a WeakSet cannot contain.
Screenshots
System
N/A — content issue, not environment-specific.
Additional context
I've already opened PR #66981 with a proposed fix, verified locally via Codespaces.
Happy to update the PR based on triage feedback.
Describe the Issue
The WeakSet example in this lesson has three correctness issues:
.add(). Because WeakSet holds only weak references, these objects have no remaining strong reference once the statement completes and are eligible for garbage collection..has()call — a literal that looks identical evaluates to a distinct object, and WeakSet compares by reference identity..has()call is made with a string argument, which will always returnfalse. WeakSet can only store objects; strings are not valid entries.Proposed fix: Refactor the example to bind objects to variables before adding them to the WeakSet, and pass one of those variables to
.has()instead of a string.Affected Page
https://www.freecodecamp.org/learn/javascript-v9/lecture-working-with-maps-and-sets/what-are-sets-in-javascript-and-how-does-it-differ-from-weaksets
Your code
Expected behavior
The lesson should demonstrate a working WeakSet example where objects are bound to variables before being added, and where
.has()is called with an object reference. The current example cannot work as shown because objects added via bare literals to.add()have no remaining reference to pass to a later.has(), and the.has()call uses a string, which a WeakSet cannot contain.Screenshots
System
N/A — content issue, not environment-specific.
Additional context
I've already opened PR #66981 with a proposed fix, verified locally via Codespaces.
Happy to update the PR based on triage feedback.