Set.prototype.has
Checks whether a value is in a Set.
Available since: ES2015
Syntax
set.has(value)Parameters
| Parameter | Type | Description |
|---|---|---|
| value | any | The value to look for. |
Return value
booleantrue if the value is in the Set.
Examples
Checking allowed values:
const allowed = new Set(['jpg', 'png', 'gif']);
console.log(allowed.has('png'));
console.log(allowed.has('exe'));Output:
true
falseNotes
On large collections set.has() is much faster than array.includes(): it does not scan elements one by one.
See also
- Set.prototype.add — Adds a value to a Set if it is not there yet.
- Array.prototype.includes — Checks whether an array contains a value.
- Map.prototype.has — Checks whether a key is in a Map.
In the tutorial
- Map and Set — The Map and Set collections in JavaScript: a dictionary with keys of any type and a set of unique values.