Set Object (JavaScript)

A collection of unique values that may be of any type.

Syntax

setObj = new Set()     

Remarks

If you try to add a non-unique value to a Set, the new value will not be added to the collection.

Properties

The following table lists the properties of the Set object.

Property Description
constructor Specifies the function that creates a set.
prototype Returns a reference to the prototype for a set.
size Returns the number of elements in a set.

Methods

The following table lists the methods of the Set object.

Method Description
add Adds an element to a set.
clear Removes all elements from a set.
delete Removes a specified element from a set.
forEach Performs the specified action for each element in a set.
has Returns true if the set contains a specified element.
toString Returns a string representation of a set.
valueOf Returns the primitive value of the specified object.

Example

The following example shows how to add members to a set and then retrieve them.

var s = new Set();  
s.add("Thomas Jefferson");  
s.add(1776);  
s.add("founding father");  

s.forEach(function (item) {  
    document.write(item.toString() + ", ");  
});  

// Output:  
// Thomas Jefferson, 1776, founding father,  

Requirements

Supported in the Internet Explorer 11 standards document mode. Also supported in Store apps (Windows 8.1 and Windows Phone 8.1). See Version Information.
Not supported in the following document modes: Quirks, Internet Explorer 6 standards, Internet Explorer 7 standards, Internet Explorer 8 standards, Internet Explorer 9 standards, Internet Explorer 10 standards. Not supported in Windows 8.