weakSet.has(value) asks one question: is this exact object in the set? It answers true or false and never changes the set. It compares by reference, so two different objects with the same contents are different. If you pass a number, string, or other non-object, you get false and no error.
Remember
const reviewed = new WeakSet();
const post = { title: "Hello" };
reviewed.add(post);
reviewed.has(post) → true
reviewed.has({ title: "Hello" }) → false (a different object)
reviewed.has("Hello") → false (not an object, no error)
Syntax
Call has on a WeakSet and pass the object you are looking for.
JavaScript
const weakSet = new WeakSet();
weakSet.has(value)
Call
What it does
weakSet.has(value)
Return true if the object is in the set, otherwise false.
if (!weakSet.has(obj)) { ... }
Run code only the first time you meet an object.
Part of ES2015. A WeakSet has no size and cannot be looped over, so has() is the only way to read from it.
Library declarations
JavaScript
weakSet.has(value) // returns true or false
// weakSet.size does not exist
// weakSet.forEach() does not exist
// for (const x of weakSet) does not work
Parameters
Parameter
Description
value
The object to look for. Any other value, such as a number, string, null, or undefined, returns false without an error.
Return Value
Return Value
Description
true
This exact object was added earlier and has not been deleted.
false
The object is not in the set, or the value is not an object.
JavaScript Examples
Example 1 — Ask if an Object Is in the Set
Mark one article as reviewed, then check it, check an unmarked one, and check again after removing it.
Same variable, same object.original points to the array that was added, so the lookup succeeds.
Twin is a stranger.twin and a fresh [1, 2] are new arrays in memory. The set only knows the one you gave add(), so both return false.
Text can be equal, objects are not. The last line turns both arrays into the string "[1,2]", which are equal. That comparison is on text, not on the objects themselves.
Example 3 — Check That an Object Came from Your Factory
Remember every point made by createPoint(). Later, isPoint() accepts only those objects and says no to everything else.
JavaScript
const made = new WeakSet();
function createPoint(x, y) {
const point = { x: x, y: y };
made.add(point);
return point;
}
function isPoint(value) {
return made.has(value);
}
const p = createPoint(1, 2);
console.log(isPoint(p)); // true
console.log(isPoint({ x: 1, y: 2 })); // false
console.log(isPoint(Object.assign({}, p))); // false
console.log(isPoint("point")); // false
console.log(isPoint(null)); // false
Stamp at creation. Every point from createPoint() is added to made before it is returned, so the factory leaves a mark that outside code cannot fake.
Copies fail the test. A hand-typed object and an Object.assign copy have the right shape, but they are not in made, so isPoint() says false.
Odd input is safe. A string and null would make add() throw, but has() just returns false, so the helper needs no extra type check.
Compatibility
Browser & Runtime Support
WeakSet.prototype.has() is part of ES2015 (ES6) and ships together with WeakSet.
✓ Baseline · ES2015
WeakSet.prototype.has()
Supported in Chrome 36+, Firefox 34+, Safari 9+, Edge 12+, Node.js, and Deno. Internet Explorer 11 has WeakMap but not WeakSet.
ES2015Supported for years
Google Chrome36 and newer
Supported
Mozilla Firefox34 and newer
Supported
Apple Safari9 and newer
Supported
Microsoft Edge12 and newer
Supported
Node.jsAll current releases
Supported
DenoAll releases
Supported
Internet ExplorerNo WeakSet, not even IE 11
Not supported
weakSet.has()Very wide
Bottom line: Safe in every modern browser and runtime. Feature-detect the whole class with typeof WeakSet === "function" if you still serve very old browsers.
Frequently Asked Questions
It returns true if the exact object is in the set, and false if it is not. It never changes the set and never returns anything else.
Objects are compared by reference, not by contents. A second object that looks the same is a different object in memory, so it is not a member. Pass the same variable you added.
No. For a number, string, boolean, null, or undefined it simply returns false. This makes has() safe to call on input you have not checked yet.
No. A WeakSet has no size property and cannot be looped over, so has() is the only way to read it. If you need a count or a list, use a regular Set.
It works wherever WeakSet works: Chrome 36, Firefox 34, Safari 9, Edge 12, Node.js, and Deno. Internet Explorer has no WeakSet. Use typeof WeakSet === "function" to detect it.