Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Set.prototype.has — checking value membership

Set.prototype.has checks whether a set contains a given value and returns true or false. It does not modify the set; values are compared using SameValueZero, and objects are compared by identity.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

set.has(value)

Call Set.prototype.has as set.has(value), where set is a Set and value is the value to find. It returns true if the value belongs to the set, or false if it does not. The method neither adds nor removes elements.

Comparison follows SameValueZero: there is no type coercion, so 1 and "1" are different. NaN matches NaN, and +0 and -0 are treated as equal. Objects are compared by identity, not by contents: a new object with identical properties does not match the stored object.

Omitting the argument searches for undefined; it does not test whether the set is empty. The method requires a receiver with the internal data of a Set; calling it with a plain object or an array as this throws a TypeError.

In the example, item is the same reference that was stored in the set, whereas { id: 1 } creates a different object. The last two results are therefore true and false, respectively.

Examples

const item = { id: 1 };
const values = new Set([1, NaN, -0, item]);

console.log(values.has(1));
console.log(values.has("1"));
console.log(values.has(NaN));
console.log(values.has(+0));
console.log(values.has(item));
console.log(values.has({ id: 1 }));

Result

true
false
true
true
true
false

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources