Technical skill practice
JavaScript Set: Track Membership and Inspect Object Identity
A Set stores unique values for membership operations. Use add to register a value, has to check it and delete to remove it. For object values, identity matters rather than an automatic comparison of fields.
The task
Track active badge labels, repeat one addition, remove a member and compare two separate record objects with the same id.
Runnable Node 22 example
const assert = require('node:assert/strict');
const active = new Set();
active.add('badge-A').add('badge-B').add('badge-A');
assert.equal(active.size, 2);
assert.equal(active.has('badge-A'), true);
assert.equal(active.delete('badge-A'), true);
assert.equal(active.delete('badge-A'), false);
assert.equal(active.has('badge-A'), false);
const first = {id: 1};
const objects = new Set([first, {id: 1}]);
assert.equal(objects.size, 2);
assert.equal(objects.has(first), true);
assert.equal(objects.has({id: 1}), false);
console.log(`active=${active.size}; objects=${objects.size}`);
Expected output and explanation
Adding badge-A twice does not increase the primitive membership count. The first deletion reports success and the second reports that there was no remaining matching value to remove. The final active size is one.
The object set retains two references. Reusing first finds a member, but constructing another object does not find one merely because id is equal. If membership is defined by an id field, store or index that key deliberately. The separate duplicate-removal exercise defines a first-record-per-id policy.
Output from the example
active=1; objects=2
Pitfall to check
A Set membership check is not a permission system by itself. The application must decide who can alter the tracker and what its values mean. This example is an in-memory data-structure experiment, not an authorization design.
Questions, answered
Can I inspect Set length like an array?
Use size for its entry count. A Set is not an array and does not use array index positions for its stored members.
Sources and examples
ResumizeAI team. Examples are fictional teaching material, not customer results. Replace sample details with your own facts.
Your experience. A complete resume.
Build in Resumize with ATS-friendly templates, guided editing and a live preview. Start free, then upgrade when you need more.
Free account required. Free-plan PDF exports include a watermark.