Set Object (JavaScript)

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


setObj = new Set()     


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


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.


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.


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("founding father");  

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

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


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.