JavaScript Set has() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Instance method

Definition

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");
}
CallWhat 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.

A standard JavaScript method (ES2015) on every Set, from Set.prototype. No import required. Siblings: add(), clear(), delete(), difference(), entries(), forEach(), intersection(), isDisjointFrom(), isSubsetOf(), isSupersetOf(), keys(), symmetricDifference(), union(), values(), [Symbol.iterator]().

Library declarations
JavaScript
// ECMAScript 2015 (ES6)
Set.prototype.has(value)

// value  : the value to look for
// returns: true or false

Parameters

ParameterDescription
valueRequired. The value to look for. Strings, numbers, booleans, null, undefined, NaN, objects and functions are all allowed.

Return Value

Return ValueDescription
trueThe value is in the Set.
falseThe value is not in the Set.

JavaScript Examples

Example 1 — Check if a Value Is in a Set

Ask a Set of allowed roles about three values, and use the answer in an if statement.

JavaScript
const allowed = new Set(["admin", "editor"]);

console.log("admin: " + allowed.has("admin"));
console.log("guest: " + allowed.has("guest"));
console.log("Admin: " + allowed.has("Admin"));

const role = "editor";
if (allowed.has(role)) {
  console.log(role + " can edit");
}
Try It Yourself

How It Works

  1. Look up values. "admin" was added to the Set, so has() returns true. "guest" was not, so the answer is false.
  2. Case counts. "Admin" with a capital A is a different string, so has() returns false.
  3. Use it in a condition. Since has() already returns a boolean, you can put it directly inside if (...).

Example 2 — Types, NaN and Zero

See how has() treats a string and a number, NaN, and the two kinds of zero.

JavaScript
const mixed = new Set([5, NaN, 0]);

console.log("Has 5: " + mixed.has(5));
console.log("Has \"5\": " + mixed.has("5"));
console.log("Has NaN: " + mixed.has(NaN));
console.log("Has -0: " + mixed.has(-0));

console.log("NaN === NaN: " + (NaN === NaN));
console.log("[NaN].includes(NaN): " + [NaN].includes(NaN));
Try It Yourself

How It Works

  1. Types matter. The Set holds the number 5, so the string "5" is not found.
  2. 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.
  3. 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(", "));
Try It Yourself

How It Works

  1. Same object. user is stored in the Set, so has(user) is true.
  2. Look-alike object. { id: 1 } written again is a new object, so has() returns false.
  3. 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.

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.

ES2015 Standard since
Google Chrome 38+ · Desktop & Mobile
Full support
Microsoft Edge 12+ · Chromium based
Full support
Mozilla Firefox 37+ · Desktop & Mobile
Full support
Apple Safari 9+ · macOS & iOS
Full support
Opera 25+ · Chromium based
Full support
Samsung Internet Recent versions · Android
Supported
Android WebView 38+ · Chromium based
Supported
Node.js 4+ · Server runtime
Supported
Deno Recent versions · JavaScript runtime
Supported
Bun Recent versions · JavaScript runtime
Supported
Internet Explorer IE 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.

Next: intersection()

Learn how to get the values that two Sets have in common, as a new Set.

intersection() tutorial →

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.

13 people found this page helpful