While Set API is still very minimalistic, you can use Array.prototype.forEach and shorten your code a bit:
array.forEach(item => mySet.add(item))
// alternative, without anonymous arrow function
array.forEach(mySet.add, mySet)
Answer from amankkg on Stack OverflowMDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Set › add
Set.prototype.add() - JavaScript - MDN Web Docs - Mozilla
The add() method of Set instances inserts the specified value into this set, if it is not already present.
14:58
#3. Set in JavaScript - Easy Explanation #javascript - YouTube
06:57
JS Sets - Javascript Programming 24 - YouTube
07:06
JavaScript Set Explained - YouTube
07:22
JavaScript Set Add() Method - YouTube
02:20
JavaScript Basics - How to use Object Sets - YouTube
15:23
Sets - Javascript In Depth - YouTube
Top answer 1 of 12
264
While Set API is still very minimalistic, you can use Array.prototype.forEach and shorten your code a bit:
array.forEach(item => mySet.add(item))
// alternative, without anonymous arrow function
array.forEach(mySet.add, mySet)
2 of 12
137
Here's a functional way, returning a new set:
const set = new Set(['a', 'b', 'c'])
const arr = ['d', 'e', 'f']
const extendedSet = new Set([ ...set, ...arr ])
// Set { 'a', 'b', 'c', 'd', 'e', 'f' }
Javatpoint
javatpoint.com › javascript-set-add-method
JavaScript Set add() Method - javatpoint
Syntax The clear() method is represented by the following syntax: setObj.clear() JavaScript example Here, we will understand clear() method through various examples. Example 1 Let's see a simple example of clear() method. <script> var set = new Set(); set.add("jQuery"); set.add("AngularJS"); set.add("Bootstrap"); document.writeln("Size before invoking...
TutorialsPoint
tutorialspoint.com › javascript › javascript_set_add_method.htm
JavaScript - Set.add() Method
This method returns the Set object itself with the added values. In the following example, we are using the JavaScript Set.add() method to insert the numbers 5 and 10 to the "numberset" set −
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Set
Set - JavaScript - MDN Web Docs
August 13, 2026 - Writeable Set-like objects additionally have the methods: clear(), delete(), and add().
Codecademy
codecademy.com › docs › javascript › sets › .add()
JavaScript | Sets | .add() | Codecademy
June 16, 2022 - Inserts a value into a Set (if it is unique) and returns the updated Set object.
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Set › values
Set.prototype.values() - JavaScript - MDN Web Docs - Mozilla
July 20, 2025 - const set = new Set(); set.add(42); set.add("forty two"); const iterator = set.values(); console.log(iterator.next().value); // Expected output: 42 console.log(iterator.next().value); // Expected output: "forty two"
Telerik
telerik.com › blogs › understanding-utilizing-javascript-set-object
Understanding and Utilizing the JavaScript Set Object
February 2, 2024 - By leveraging the Set's built-in methods for adding, checking and removing values, developers can simplify their code and reduce the complexity of handling distinct values. Moreover, the Set object ensures uniqueness, maintains insertion order and provides efficient value existence checks, which makes it a powerful tool in specific use cases. A Map object in JavaScript ...
Reality Ripple
udn.realityripple.com › docs › Web › JavaScript › Reference › Global_Objects › Set › add
Set.prototype.add() - JavaScript - UDN Web Docs: MDN Backup
The add() method appends a new element with a specified value to the end of a Set object.
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Set › has
Set.prototype.has() - JavaScript - MDN Web Docs
September 22, 2025 - const mySet = new Set(); mySet.add("foo"); console.log(mySet.has("foo")); // true console.log(mySet.has("bar")); // false const set = new Set(); const obj = { key1: 1 }; set.add(obj); console.log(set.has(obj)); // true console.log(set.has({ key1: 1 })); // false, because they are different object references console.log(set.add({ key1: 1 })); // now set contains 2 entries
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Set › Set
Set() constructor - JavaScript - MDN Web Docs
September 13, 2025 - 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);
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.