# Set.has()

> The `.has()` JS method returns a boolean indicating whether an element is present in the `Set` object or not.

## How do I check if a Set contains a value in JavaScript?

The method that checks if a specific element exists in the `Set` object. It returns `true` if the element is found and `false` otherwise.

- Topic: Set
- Type: method
- Browser support: Chrome 38+, Chrome Android 38+, Edge 12+, Firefox 13+, Firefox Android 14+, Safari 8+, Safari iOS 8+

## Example

Wrong:

```js
const result = items.filter(
  (x) => blocked.includes(x.id)
);
```

Right:

```js
const blockedSet = new Set(blocked);
const result = items.filter((x) => blockedSet.has(x.id));
```

`includes()` scans `blocked` for every item; a `Set` lookup avoids that repeated scan.

## Common mistakes

- `set.has({ id: 1 })` returns `false` for an object that only looks the same.

## Key points

- Repeated membership checks use `set.has()` instead of `array.includes()` inside a loop.
- Object lookups pass the same reference that was added, not a copy.

## Interview questions

### Is `Set.has()` faster than `Array.includes()`?

For large or repeated checks, yes. `has()` is close to constant time on average, while `includes()` scans the array.

### Does `has()` find `NaN`?

Yes. Sets use SameValueZero, so `new Set([NaN]).has(NaN)` is `true`.

## Related cards

- [Set](https://wonderium.dev/js/set.md)
- [Array.includes()](https://wonderium.dev/js/array-includes.md)
- [Map.has()](https://wonderium.dev/js/map-has.md)
- [Hash Table](https://wonderium.dev/algorithms/hash-table.md)
- [O(1)](https://wonderium.dev/algorithms/o-1.md)

---

- Level: [JavaScript](https://wonderium.dev/js.md)
- HTML version: https://wonderium.dev/js/set-has
- Last updated: 2026-11-25
