ES6 provides a native Set:

let s = new Set();
let a = {};
let b = {};

s.add(a);

console.log(s.has(a));  // true
console.log(s.has(b));  // false
Run code snippetEdit code snippet Hide Results Copy to answer Expand

Answer from Ry- on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Set
Set - JavaScript - MDN Web Docs
August 13, 2026 - The Set object lets you store unique values of any type, whether primitive values or object references.
Discussions

What the heck the "new Set()" is?
It’s kinda returns an object, but objects have a pair prop: 'val', but ‘Set’ has only one prop/val(?)… When I do console.log(new Set([1,2,3,4,4,5,5,5,6])) in my console I have this result Set(5) {1, 2, 3, 4, 5,6}. As I remember it’s one of the ES6 feature but I’m not sure. More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
5
0
June 19, 2022
[AskJS] When do you use the Set object?
Remove duplicate entries More on reddit.com
🌐 r/javascript
5
3
April 20, 2020
Are JS maps and sets really that ...useless?
Your article does a good job of pointing out the cases where you want maps and sets. For maps, it lets you use non-strings as keys, and it remembers the insertion order. For sets, you also get the removal of duplicates. So if you need that stuff, maps and sets are good tools. If you don't need that stuff, then you don't. Objects still are the right choice for most cases, because most cases don't need those extra features. And with fewer features, objects can specialize and get better performance for some cases. More on reddit.com
🌐 r/learnjavascript
5
10
July 6, 2021
I discovered Set exists for the first time today... Is it really faster?
I use sets, maps, and spread syntax all the time. I’ve never “done the math” to see the specs of the speed for sets. Maps have methods that are way faster when trying to search and filter things in big data sets. More on reddit.com
🌐 r/learnjavascript
26
11
July 24, 2024
🌐
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.
🌐
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.
🌐
Attacomsian
attacomsian.com › blog › sets-javascript
Introduction to Set object in JavaScript
October 8, 2022 - A Set is a new object type introduced in ES6 that allows you to create collections of values. A value can be either a primitive or an object and can occur only once in the set; it is unique in the collection.
🌐
DEV Community
dev.to › keevcodes › the-javascript-set-object-2f75
The JavaScript Set Object - DEV Community
June 27, 2019 - Set is an object that can store any type of JavaScript primitive (think booleans, strings, numbers here), but it will only store unique primitives. Wait... what??? That's awesome!
🌐
Medium
medium.com › @emad-mohamed › objects-vs-set-on-javascript-583ca1511985
Objects vs Set on JavaScript. Objects | by Emad Mohamed | Medium
September 16, 2024 - This is because JavaScript object keys are always strings, even if you use numeric literals. So, obj essentially looks like this in a more visual format: ... A Set is a collection of unique values where each value can be of any type.
Find elsewhere
🌐
Plain English
plainenglish.io › home › blog › javascript › understanding the set object in javascript
Understanding The Set Object in JavaScript
December 1, 2012 - The set object in JavaScript is an ES6 feature that lets you store unique values, primitives, or object references. The Set objects are collections of values. You can iterate through the elements of a set in insertion order.
🌐
YouTube
youtube.com › watch
Sets in Javascript (Set Object) - YouTube
Similar to Arrays, a Set can be used to store a sequence of values in Javascript. However, a Set can only store unique values, meaning we cannot store duplic...
Published: December 24, 2017
🌐
TypeOfNaN
typeofnan.dev › the-javascript-set-object
The JavaScript Set Object | TypeOfNaN
Today I’ll introduce the Set object and discuss some of its use cases. The Set object is a collection of values in which you can store unique primitive values or object references.
🌐
freeCodeCamp
forum.freecodecamp.org › curriculum help
What the heck the "new Set()" is? - Curriculum Help - The freeCodeCamp Forum
June 19, 2022 - It’s kinda returns an object, but objects have a pair prop: 'val', but ‘Set’ has only one prop/val(?)… When I do console.log(new Set([1,2,3,4,4,5,5,5,6])) in my console I have this result Set(5) {1, 2, 3, 4, 5,6}. As I…
🌐
DEV Community
dev.to › nas5w › learn-the-javascript-set-object-1f86
Learn JavaScript Sets (Simple Yet Powerful Built-In Objects) - DEV Community
March 21, 2020 - Today I'll introduce the Set object and discuss some of its use cases. The Set object is an object in which you can store unique primitive values or object references.
🌐
Reddit
reddit.com › r/javascript › [askjs] when do you use the set object?
r/javascript on Reddit: [AskJS] When do you use the Set object?
April 20, 2020 -

The author of this awesome post deleted their description. Unfortunately, commenters were agreeing that they used Set in the same scenarios as OP. The problem here is OP removed the post, so I can't see what they are agreeing with.

So, when should a Set be used, in your opinion?

Top answer
1 of 4
5
Remove duplicate entries
2 of 4
4
Set is similar to Array. It's faster, but more limited. The main difference is Set does not allow duplicates. If I wanted to replicate Set with Arrays, it would look like this: const SetLike = { _array: [], has: function(value) { return this._array.includes(value); }, add: function(value) { if (!this.has(value)) [].push(value); }, delete: function(value) { let idx = this._array.indexOf(value); if (idx !== -1) { this._array.splice(idx, 1); } }, clear: function() { this._array.splice(0, this._array.length); }, keys: function() { return this._array[Symbol.iterator]; } }; You can use them when you don't care about the order of items and just want to see if something is included in a list. It's essentially for collections, or even tagging. It's worth nothing that Set works with primitives like numbers and strings whereas WeakSet does not. An Object in a Set will be held in memory, much like if it's in an Array. WeakSet can't be iterated, but will tell you if an Object exists inside it's set. But adding an Object to a WeakSet will still let it get garbage selected. This is good for stuff like HTMLElement, where you want the element to be destroyed by the DOM and have no references. You can use that for "tagging" . Instead of doing element.dataset.isDirty = 'true', you can do myWeakSetOfDirtyElements.add(element). Then you can check via myWeakSetOfDirtyElements.has(element). Essentially, tagging Objects with a boolean. You aren't holding it in an Array, meaning the DOM can delete and it disappears from your WeakSet (not that you'd notice). Map is similar to Set, but each element has an associated value. It's very similar to using an {} where you use keys and values, but it's overall faster. WeakMap has the same loose reference (allowing garbage collection). So you can get an expanded value instead rather than a simple boolean representation of being included or not. In my opinion, WeakSet and WeakMap are the where the true strengths lie. Set and Map can be replicated, more or less, with Array and Object, respectively.
🌐
JavaScript in Plain English
javascript.plainenglish.io › javascript-a-deep-dive-into-sets-489b7a3dc06d
JavaScript: A Deep Dive into Sets | by Nicky Christensen | JavaScript in Plain English
January 29, 2022 - Sets allow you to create collections of unique values and are also iterable like arrays. A set can hold values like simple primitives, strings, or it can hold objects literals or arrays.
🌐
Medium
medium.com › @t.hasan.prince › javascript-set-objects-when-and-how-to-use-them-ce2642628503
JavaScript Set Objects: When and How to Use Them | by Tanvir Hasan Prince | Medium
July 2, 2024 - In JavaScript, a Set is a built-in object that lets you store unique values of any type, whether primitive values or object references…
🌐
DEV Community
dev.to › imamdev_ › javascript-set-every-junior-should-know-2op8
JavaScript Set, Every Junior Should Know - DEV Community
November 16, 2022 - The Set constructor creates a new Set object. You can pass an iterable object as an argument to the constructor. The iterable object's elements will be added to the new Set.
🌐
TutorialsPoint
tutorialspoint.com › javascript › javascript_sets_object.htm
JavaScript - Sets
A JavaScript Set object is a collection of unique values. Each value can only occur once in a Set. A Set can hold any value of any data type. The sets are introduced to JavaScript in ECMAScript 6 (ES6).
🌐
DEV Community
dev.to › firebase007 › demystifying-the-set-object-3k9p
Understanding the new Set object in JavaScript - DEV Community
January 17, 2020 - On the other hand, if nothing is passed to the Set object or if its value is a primitive like null for example, its return value will be empty. Moving on, just like every object has a constructor function, where they derive their traits from including methods and properties, all instances of the Set object inherit from the Set prototype.
🌐
MakeUseOf
makeuseof.com › home › programming › 8 javascript set methods you should master today
8 JavaScript Set Methods You Should Master Today
October 30, 2021 - A JavaScript Set is a collection of values that are unique. Sets can hold values of any data type. It can be simple primitives, such as integers or strings, etc, or complex types like arrays or object literals.