set.has(value) checks whether a value is inside a Set. It returns true if the value is there and false if it is not. It never changes the Set.
It is the main reason to choose a Set over an array for lookups. has() is built to be fast even when the Set holds many values, while array.includes() may have to look at every item.
Remember
set.has(value)
value is in the Set → true
value is not there → false
Same type needed: "5" is not 5
NaN is found (unlike with ===)
Objects match only if they are the same object
Syntax
Call it on a Set and pass the value to look for. Use the answer in an if statement.
JavaScript
const allowed = new Set(["admin", "editor"]);
console.log(allowed.has("admin")); // true
console.log(allowed.has("guest")); // false
if (allowed.has("editor")) {
console.log("Access granted");
}
Call
What it does
set.has(value)
Returns true or false.
!set.has(value)
True when the value is missing, for example to add it only once.
array.includes(value)
The array version. It also finds NaN, but it checks items one by one.
set.add(value)
Add a value. You do not need has() first, because a Set ignores duplicates.
Has 5: true
Has "5": false
Has NaN: true
Has -0: true
NaN === NaN: false
[NaN].includes(NaN): true
How It Works
Types matter. The Set holds the number 5, so the string "5" is not found.
NaN works. A normal NaN === NaN is false, but a Set treats NaN as equal to itself, so has(NaN) is true. Zero and -0 are also treated as the same value.
Compare with arrays.includes() finds NaN too. The difference is speed: a Set is designed for fast lookups on large data.
Example 3 — Objects and Unique Items
Objects are found by identity. Then has() is used to remove duplicates from a list while keeping the first of each.
JavaScript
const user = { id: 1 };
const users = new Set([user]);
console.log("Same object: " + users.has(user));
console.log("Look-alike: " + users.has({ id: 1 }));
const names = ["Ann", "Bob", "Ann", "Cy", "Bob"];
const seen = new Set();
const unique = [];
for (const name of names) {
if (!seen.has(name)) {
seen.add(name);
unique.push(name);
}
}
console.log("Unique: " + unique.join(", "));
Same object: true
Look-alike: false
Unique: Ann, Bob, Cy
How It Works
Same object.user is stored in the Set, so has(user) is true.
Look-alike object.{ id: 1 } written again is a new object, so has() returns false.
Remember what you saw. The loop adds a name to seen the first time and skips it later, so unique keeps one copy of each name, in order.
Compatibility
Browser & Runtime Support
Set.prototype.has() is part of ES2015 (ES6), together with Set itself. Every current browser and Node.js has it.
✓ ES2015 · Widely supported
set.has()
Supported in Chrome 38+, Edge 12+, Firefox 37+, Safari 9+, Opera 25+ and Node.js 4+. Internet Explorer 11 only has a basic Set.
ES2015Standard since
Google Chrome38+ · Desktop & Mobile
Full support
Microsoft Edge12+ · Chromium based
Full support
Mozilla Firefox37+ · Desktop & Mobile
Full support
Apple Safari9+ · macOS & iOS
Full support
Opera25+ · Chromium based
Full support
Samsung InternetRecent versions · Android
Supported
Android WebView38+ · Chromium based
Supported
Node.js4+ · Server runtime
Supported
DenoRecent versions · JavaScript runtime
Supported
BunRecent versions · JavaScript runtime
Supported
Internet ExplorerIE 11 · basic Set only
Partial support
set.has()Excellent
Bottom line: Safe to use in every current browser and in Node.js. Only Internet Explorer needs special care.
Frequently Asked Questions
It returns true if the value is in the Set and false if it is not. It does not change the Set.
For large collections, yes. A Set is built for quick lookups, while includes() may check every item one by one. For a few items you will not see a difference.
Yes. A Set treats NaN as equal to NaN, so new Set([NaN]).has(NaN) is true, even though NaN === NaN is false. Zero and -0 are also treated as the same value.
Objects are compared by identity, not by content. A new object with the same properties is a different object. Keep a reference to the stored object and pass that same reference to has().
No. add() ignores a value that is already in the Set, so you can call it directly. Use has() when you want to know if the value was new, or to do something different for repeats.