JavaScript WeakSet has() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
WeakSet

Definition

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)
CallWhat 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

ParameterDescription
valueThe object to look for. Any other value, such as a number, string, null, or undefined, returns false without an error.

Return Value

Return ValueDescription
trueThis exact object was added earlier and has not been deleted.
falseThe 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.

JavaScript
const reviewed = new WeakSet();
const article = { title: "WeakSet" };
const draft = { title: "Map" };
reviewed.add(article);

console.log(reviewed.has(article));  // true
console.log(reviewed.has(draft));    // false
reviewed.delete(article);
console.log(reviewed.has(article));  // false
Try It Yourself

How It Works

  1. A match is true. article was added, so reviewed.has(article) answers true.
  2. No match is false. draft was never added, so the question gets a plain false instead of an error.
  3. Answers can change. After delete(article) the same question returns false. has() reports the set as it is right now.

Example 2 — Equal Contents Do Not Match

Two arrays hold the same numbers, but only the one you added counts as a member.

JavaScript
const set = new WeakSet();
const original = [1, 2];
const twin = [1, 2];
set.add(original);

console.log(set.has(original));  // true
console.log(set.has(twin));      // false
console.log(set.has([1, 2]));    // false
console.log(JSON.stringify(original) === JSON.stringify(twin));  // true
Try It Yourself

How It Works

  1. Same variable, same object. original points to the array that was added, so the lookup succeeds.
  2. 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.
  3. 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
Try It Yourself

How It Works

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

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.

ES2015 Supported for years
Google Chrome 36 and newer
Supported
Mozilla Firefox 34 and newer
Supported
Apple Safari 9 and newer
Supported
Microsoft Edge 12 and newer
Supported
Node.js All current releases
Supported
Deno All releases
Supported
Internet Explorer No 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.

Back to the WeakSet Method Index

See every WeakSet method in one place, plus what a WeakSet does not have.

WeakSet methods →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful