Set.prototype.has()
set.has(value) Returns:
boolean · Updated March 13, 2026 · Map and Set set has contains
The has() method returns a boolean indicating whether a Set contains a specific value.
Syntax
set.has(value)
Parameters
| Parameter | Type | Description |
|---|---|---|
| value | any | The value to test for existence in the Set |
Return Value
Returns true if the value exists in the Set; otherwise returns false.
Description
The has() method is the primary way to check for element existence in a Set. Unlike arrays where you might use Arrayincludes() or ArrayindexOf(), Set membership checks are constant time — O(1) — regardless of how many elements the Set contains.
This makes Sets dramatically faster for membership testing, especially with large collections. While an array search scales linearly (O(n)) with each additional element, a Set lookup takes the same time whether it contains 10 or 10 million items.
Equality Semantics
has() uses strict equality (===) to compare values:
- Primitives (numbers, strings, booleans,
null,undefined,Symbol, BigInt): Compared by value.1and'1'are different. - Objects: Compared by reference, not by value. Two objects with identical properties are not equal unless they reference the exact same object in memory.
- Special case:
NaNis treated as equal to anotherNaN, which differs from===behavior whereNaN === NaNreturnsfalse.
Examples
Basic existence check
const colors = new Set(['red', 'green', 'blue']);
console.log(colors.has('red'));
// true
console.log(colors.has('yellow'));
// false
console.log(colors.has('RGB'));
// false (case-sensitive)
Checking with objects and references
const user1 = { id: 1, name: 'Alice' };
const user2 = { id: 2, name: 'Bob' };
const users = new Set([user1]);
console.log(users.has(user1));
// true (same reference)
console.log(users.has({ id: 1, name: 'Alice' }));
// false (different object, different reference)
const id = { id: 1 };
users.add(id);
console.log(users.has(id));
// true (same reference)
Performance comparison with Array.includes()
// Create a large dataset
const values = Array.from({ length: 10000 }, (_, i) => i);
const set = new Set(values);
const array = [...values];
const target = 9999;
// Array.includes() - O(n) linear search
console.time('Array.includes');
array.includes(target);
console.timeEnd('Array.includes');
// ~0.3ms (scales with array size)
// Set.has() - O(1) constant time
console.time('Set.has');
set.has(target);
console.timeEnd('Set.has');
// ~0.01ms (constant, regardless of Set size)
// With larger datasets, the difference becomes dramatic
// Array: 10ms → 100ms → 1000ms as size grows
// Set: 0.01ms → 0.01ms → 0.01ms (stable)
Common Patterns
Tracking visited items
const visited = new Set();
function visit(url) {
if (visited.has(url)) {
console.log('Already visited:', url);
return false;
}
visited.add(url);
return true;
}
visit('https://example.com');
// true
visit('https://example.com');
// Already visited: https://example.com
// false
Preventing duplicates in processing
const users = [
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' },
{ id: 1, name: 'Alice' } // duplicate
];
const seenIds = new Set();
const unique = users.filter(user => {
if (seenIds.has(user.id)) return false;
seenIds.add(user.id);
return true;
});
console.log(unique.length);
// 2
Set membership before operations
const allowedActions = new Set(['read', 'write', 'delete']);
function performAction(user, action) {
if (!allowedActions.has(action)) {
console.log(`Invalid action: ${action}`);
return false;
}
console.log(`${user} performed ${action}`);
return true;
}
performAction('Alice', 'read');
// Alice performed read
performAction('Bob', 'execute');
// Invalid action: execute