Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. JavaScript
  3. Reference
  4. Standard built-in objects
  5. Set
  6. has()

Set.prototype.has()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨July 2015⁩.

Thehas() method ofSet instances returns a boolean indicating whether the specified value exists in thisSet or not.

Try it

const set = new Set([1, 2, 3, 4, 5]);console.log(set.has(1));// Expected output: trueconsole.log(set.has(5));// Expected output: trueconsole.log(set.has(6));// Expected output: false

Syntax

js
has(value)

Parameters

value

The value to test for presence in theSet object.

Return value

Returnstrue if the specified value exists in theSet object; otherwisefalse.

Examples

Using has()

js
const mySet = new Set();mySet.add("foo");console.log(mySet.has("foo")); // trueconsole.log(mySet.has("bar")); // falseconst set = new Set();const obj = { key1: 1 };set.add(obj);console.log(set.has(obj)); // trueconsole.log(set.has({ key1: 1 })); // false, because they are different object referencesconsole.log(set.add({ key1: 1 })); // now set contains 2 entries

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-set.prototype.has

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp