Sets are now available in ES2015 (aka ES6, i.e. ECMAScript 6). ES6 has been the current standard for JavaScript since June 2015.

ECMAScript 6 has the data structure Set which works for arbitrary values, is fast and handles NaN correctly. -Axel Rauschmayer, Exploring ES6

First two examples from Axel Rauschmayer's book Exploring ES6:

Managing single elements:

> let set = new Set();
> set.add('red')

> set.has('red')
true
> set.delete('red')
true
> set.has('red')
false

Determining the size of a Set and clearing it:

> let set = new Set();
> set.add('red')
> set.add('green')

> set.size
2
> set.clear();
> set.size
0

I would check out Exploring ES6 if you want to learn more about Sets in JavaScript. The book is free to read online, but if you would like to support the author Dr. Axel Rauschmayer you can purchase the book for around $30.

If you want to use Sets and ES6 now you can use Babel, the ES6 to ES5 transpiler, and its polyfills.

Edit: As of June 6th, 2017 most of the major browsers have full Set support in their latest versions (except IE 11). This means you may not need babel if you don't care to support older browsers. If you want to see compatibility in different browsers including your current browser check Kangax's ES6 compatibility table.

EDIT:

Just clarification on initialization. Sets can take any synchronous iterable in their constructor. This means they can take not just arrays but also strings, and iterators. Take for example the following array and string initialization of a set:

const set1 = new Set(['a','a','b','b','c','c']);
console.log(...set1);
console.log(set1.size);
const set2 = new Set("aabbcc");
console.log(...set2);
console.log(set2.size);

Both outputs of the array and string are the same. Note that ...set1 is the spread syntax. It appears that each element of the iterable is added one by one to the set, so since both the array and string have the same elements and since the elements are in the same order the set is created the same. Another thing to note about sets is when iterating over them the iteration order follows the order that the elements were inserted into the set. Here's an example of iterating over a set:

const set1 = new Set(['a','a','b','b','c','c']);
for(const element of set1) {
  console.log(element);
}

Since you can use any iterable to initialize a set you could even use a iterator from a generator function. Here is two such examples of iterator initializations that produce the same output:

// a simple generator example
function* getLetters1 () {
  yield 'a';
  yield 'a';
  yield 'b';
  yield 'b';
  yield 'c';
  yield 'c';
}

// a somewhat more commonplace generator example
// with the same output as getLetters1.
function* getLetters2 (letters, repeatTimes) {
  for(const letter of letters) {
    for(let i = 0; i < repeatTimes; ++i) { 
      yield letter;
    }
  }
}

console.log("------ getLetters1 ------");
console.log(...getLetters1());
const set3 = new Set(getLetters1());
console.log(...set3);
console.log(set3.size);

console.log("------ getLetters2 ------");
console.log(...getLetters2('abc', 2));
const set4 = new Set(getLetters2('abc', 2));
console.log(...set4);
console.log(set4.size);

These examples' generator functions could just be written to not repeat, but if the generator function is more complicated and as long as the following doesn't impact performance too negatively you could use the Set method to help get only values from a generator that don't repeat.

If you want to know more about sets without reading Dr. Rauschmayer's chapter of his book you can check out the MDN docs on Set. MDN also has more examples of iterating over a set such as using forEach and using the .keys, .values, and .entries methods. MDN also has examples such as set union, set intersection, set difference, symmetric set difference, and set superset checking. Hopefully most of those operations will become available in JavaScript without needing to build your own functions supporting them. In fact, there is this TC39 proposal for new Set methods which should hopefully add the following methods to Set in JavaScript at some future point in time if the proposal reaches stage 4:

  • Set.prototype.intersection(iterable) - method creates new Set instance by set intersection operation.
  • Set.prototype.union(iterable) - method creates new Set instance by set union operation.
  • Set.prototype.difference(iterable) - method creates new Set without elements present in iterable.
  • Set.prototype.symmetricDifference(iterable) - returns Set of elements found only in either this or in iterable.
  • Set.prototype.isSubsetOf(iterable)
  • Set.prototype.isDisjointFrom(iterable)
  • Set.prototype.isSupersetOf(iterable)
Answer from John on Stack Overflow
🌐
W3Schools
w3schools.com › js › js_sets.asp
JavaScript Sets
JS Examples JS HTML DOM JS HTML Input JS HTML Objects JS HTML Events JS Browser JS Editor JS Exercises JS Quiz JS Website JS Syllabus JS Study Plan JS Interview Prep JS Certificate ... A JavaScript Set is a collection of unique values.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Set
Set - JavaScript - MDN Web Docs
August 13, 2026 - The allowed types are set in the specification IDL definition. For example, GPUSupportedFeatures is a Set-like object that must use strings as the key/value.
Top answer
1 of 9
126

Sets are now available in ES2015 (aka ES6, i.e. ECMAScript 6). ES6 has been the current standard for JavaScript since June 2015.

ECMAScript 6 has the data structure Set which works for arbitrary values, is fast and handles NaN correctly. -Axel Rauschmayer, Exploring ES6

First two examples from Axel Rauschmayer's book Exploring ES6:

Managing single elements:

> let set = new Set();
> set.add('red')

> set.has('red')
true
> set.delete('red')
true
> set.has('red')
false

Determining the size of a Set and clearing it:

> let set = new Set();
> set.add('red')
> set.add('green')

> set.size
2
> set.clear();
> set.size
0

I would check out Exploring ES6 if you want to learn more about Sets in JavaScript. The book is free to read online, but if you would like to support the author Dr. Axel Rauschmayer you can purchase the book for around $30.

If you want to use Sets and ES6 now you can use Babel, the ES6 to ES5 transpiler, and its polyfills.

Edit: As of June 6th, 2017 most of the major browsers have full Set support in their latest versions (except IE 11). This means you may not need babel if you don't care to support older browsers. If you want to see compatibility in different browsers including your current browser check Kangax's ES6 compatibility table.

EDIT:

Just clarification on initialization. Sets can take any synchronous iterable in their constructor. This means they can take not just arrays but also strings, and iterators. Take for example the following array and string initialization of a set:

const set1 = new Set(['a','a','b','b','c','c']);
console.log(...set1);
console.log(set1.size);
const set2 = new Set("aabbcc");
console.log(...set2);
console.log(set2.size);

Both outputs of the array and string are the same. Note that ...set1 is the spread syntax. It appears that each element of the iterable is added one by one to the set, so since both the array and string have the same elements and since the elements are in the same order the set is created the same. Another thing to note about sets is when iterating over them the iteration order follows the order that the elements were inserted into the set. Here's an example of iterating over a set:

const set1 = new Set(['a','a','b','b','c','c']);
for(const element of set1) {
  console.log(element);
}

Since you can use any iterable to initialize a set you could even use a iterator from a generator function. Here is two such examples of iterator initializations that produce the same output:

// a simple generator example
function* getLetters1 () {
  yield 'a';
  yield 'a';
  yield 'b';
  yield 'b';
  yield 'c';
  yield 'c';
}

// a somewhat more commonplace generator example
// with the same output as getLetters1.
function* getLetters2 (letters, repeatTimes) {
  for(const letter of letters) {
    for(let i = 0; i < repeatTimes; ++i) { 
      yield letter;
    }
  }
}

console.log("------ getLetters1 ------");
console.log(...getLetters1());
const set3 = new Set(getLetters1());
console.log(...set3);
console.log(set3.size);

console.log("------ getLetters2 ------");
console.log(...getLetters2('abc', 2));
const set4 = new Set(getLetters2('abc', 2));
console.log(...set4);
console.log(set4.size);

These examples' generator functions could just be written to not repeat, but if the generator function is more complicated and as long as the following doesn't impact performance too negatively you could use the Set method to help get only values from a generator that don't repeat.

If you want to know more about sets without reading Dr. Rauschmayer's chapter of his book you can check out the MDN docs on Set. MDN also has more examples of iterating over a set such as using forEach and using the .keys, .values, and .entries methods. MDN also has examples such as set union, set intersection, set difference, symmetric set difference, and set superset checking. Hopefully most of those operations will become available in JavaScript without needing to build your own functions supporting them. In fact, there is this TC39 proposal for new Set methods which should hopefully add the following methods to Set in JavaScript at some future point in time if the proposal reaches stage 4:

  • Set.prototype.intersection(iterable) - method creates new Set instance by set intersection operation.
  • Set.prototype.union(iterable) - method creates new Set instance by set union operation.
  • Set.prototype.difference(iterable) - method creates new Set without elements present in iterable.
  • Set.prototype.symmetricDifference(iterable) - returns Set of elements found only in either this or in iterable.
  • Set.prototype.isSubsetOf(iterable)
  • Set.prototype.isDisjointFrom(iterable)
  • Set.prototype.isSupersetOf(iterable)
2 of 9
32

I use dict objects as sets. This works with strings and numbers, but I suppose would cause problems if you wanted to have a set of objects using custom equality and comparison operators:

Creating a set:

var example_set = 
{
    'a':true,
    'b':true,
    'c':true
}

Testing for inclusion in a set

if( example_set['a'] ){
    alert('"a" is in set');
}

Adding an element to a set

example_set['d'] = true;

Removing an element from a set

delete example_set['a'];

🌐
W3Schools
w3schools.com › js › js_set_methods.asp
JavaScript Set Methods
Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, Python, PHP, Bootstrap, Java, XML and more.
🌐
Mozilla
developer.mozilla.org › en-US › blog › javascript-set-methods
New JavaScript Set methods - MDN Web Docs - Mozilla
June 24, 2024 - A set is similar to an Array, except that each value can only be stored once. For example, we can take a list of items, add them all to a set, and then inspect the results of the set. The list on the right is the contents of the <ol> list on the left, but converted to a set.
🌐
W3Schools
w3schools.com › jsref › jsref_obj_set.asp
JavaScript Set Reference
Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, Python, PHP, Bootstrap, Java, XML and more.
🌐
Medium
noncodersuccess.medium.com › exploring-javascript-sets-practical-use-cases-and-methods-demystified-deep-dive-into-set-c16a5c5c883f
Exploring JavaScript Sets | Practical Use Cases and Methods Demystified | Deep Dive into Set Methods | JavaScript Set Mastery | by NonCoderSuccess | Medium
April 29, 2024 - We’ve covered a lot of ground, from understanding what Sets are to mastering their various methods for manipulating data. As you’ve seen, JavaScript Sets offer a versatile and efficient way to work with collections of unique values.
🌐
freeCodeCamp
freecodecamp.org › news › set-and-map-in-javascript
How to Use Set and Map in JavaScript
February 10, 2023 - You can then use a for loop or for-of loop to loop through the values. The example below shows the creation of a set of fruits containing 5 elements, followed by the use of the "entries" method to loop through all elements of the array.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › sets-in-javascript
Set in JavaScript - GeeksforGeeks
Adds the new element with a specified value at the end of the Set object. ... Example : In this example, we have added two numbers 23 and 12 into the set.
Published: September 25, 2025
🌐
JavaScripture
javascripture.com › Set
Set JavaScript API
JavaScripture · Contribute via GitHub Feedback · constructor iterable · Sets are a collection of Objects where each object can only appear once in the set. ECMAScript 2015 Spec · new Set() : Set · Creates an empty Set. Run · Spec · new Set(iterable : Object) : Set ·
🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript tutorial › the beginner’s guide to javascript set type in es6
The Beginner's Guide to JavaScript Set Type in ES6
October 6, 2023 - Here is an example: let computer = {type: 'laptop'}; let server = {type: 'server'}; let equipment = new WeakSet([computer, server]); if (equipment.has(server)) { console.log('We have a server'); }Copy · Output · We have a serverCopy · In ...
🌐
Telerik
telerik.com › blogs › understanding-utilizing-javascript-set-object
Understanding and Utilizing the JavaScript Set Object
February 2, 2024 - A Set object in JavaScript is a collection of unique values, where each value can only occur once within the set. It provides an efficient way to store, retrieve and manipulate distinct values without worrying about duplicates.
🌐
Programiz
programiz.com › javascript › set-weakset
JavaScript Set and WeakSet
You can access Set elements using the values() method and check if there is an element inside Set using has() method. For example,
🌐
Javatpoint
javatpoint.com › javascript-set
JS Set
In this tutorial, we will learn, discuss, and understand the JavaScript form. We will also see the implementation of the JavaScript form for different purposes. Here, we will learn the method to access the form, getting elements as the JavaScript form's value, and submitting the form.
🌐
Codecademy
codecademy.com › docs › javascript › map › .set()
JavaScript | Map | .set() | Codecademy
March 11, 2024 - Key-value pairs are stored in a Map object using the .set() method. ... The example above uses .set() to store two entries in the fruits map: one for 5 apples and another for 8 oranges.
🌐
W3Schools
w3schools.com › jsref › jsref_set_values.asp
JavaScript Set values() Method
More Examples Below ! ... The values() method returns an Iterator object with the values in a set. The values() method does not change the original set. ... // Create a Set const letters = new Set(["a","b","c"]); // List all Values let text = ""; for (const entry of letters.values()) { text += entry; } Try it Yourself » · set.values() is an ECMAScript6 (ES6 2015) feature. JavaScript 2015 is supported in all browsers since June 2017:
🌐
Programiz
programiz.com › javascript › examples › set-operations
JavaScript Program to Illustrate Different Set Operations
// perform union operation // contain ... } return unionSet } // two sets of fruits const setA = new Set(['apple', 'mango', 'orange']); const setB = new Set(['grapes', 'apple', 'banana']); const result = union(setA, setB); console.log(result);...
🌐
TutorialsPoint
tutorialspoint.com › javascript › javascript_sets_object.htm
JavaScript - Sets
Users can follow the syntax below to define a set in JavaScript − ... In the above syntax, we used the Set() constructor function with a new keyword to define a set. Iterable − It is an optional parameter. The Set() constructor function traverses through the elements of the iterable and creates a new set using those elements. In the example below, we have passed the array containing the duplicate elements as an argument of the Set() constructor function.
🌐
DEV Community
dev.to › draczihper › set-implementation-in-javascript-2626
Set Implementation In JavaScript - DEV Community
September 28, 2024 - Or you might have seen the below snippet in some of Javascript codes. const mySet = new Set(); mySet.add("apple"); console.log(mySet.has("apple")); // OUTPUT: True mySet.delete("apple"); console.log(mySet.has(apple)); // OUTPUT: False