Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

Set() constructor

BaselineWidely available

TheSet() constructor createsSet objects.

Try it

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

Syntax

js
new Set()new Set(iterable)

Note:Set() can only be constructed withnew. Attempting to call it withoutnew throws aTypeError.

Parameters

iterableOptional

If aniterable object is passed, all of its elements will be added to the newSet.

If you don't specify this parameter, or its value isnull, the newSet is empty.

Return value

A newSet object.

Examples

Using theSet object

js
const mySet = new Set();mySet.add(1); // Set [ 1 ]mySet.add(5); // Set [ 1, 5 ]mySet.add(5); // Set [ 1, 5 ]mySet.add("some text"); // Set [ 1, 5, 'some text' ]const o = { a: 1, b: 2 };mySet.add(o);

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-set-constructor

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp