Inaccurate WeakSet example in "What are Sets in JavaScript and how does it differ from WeakSets" lesson · Issue #66982 · freeCodeCamp/freeCodeCamp · GitHub
Skip to content

Inaccurate WeakSet example in "What are Sets in JavaScript and how does it differ from WeakSets" lesson #66982

@yoehae

Description

@yoehae

Describe the Issue

The WeakSet example in this lesson has three correctness issues:

  1. 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.
  2. 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.
  3. 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

Image

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    js v9 certThis is for the JS V9 certification.scope: curriculumLessons, Challenges, Projects and other Curricular Content in curriculum directory.status: PR in worksWork in Progress (WIP) Issues.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions