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 Overflow 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' }
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Set
Set - JavaScript - MDN Web Docs
August 13, 2026 - You can iterate through the elements of a set in insertion order. The insertion order corresponds to the order in which each element was inserted into the set by the add() method successfully (that is, there wasn't an identical element already in the set when add() was called).
11:04
An array of ways ... to create arrays (JS Basics) - YouTube
01:46
JavaScript tips — Convert a Set to an Array - YouTube
JavaScript How To Add To An Array (js) - YouTube
02:01
JavaScript tips — Add multiple values to an array using Array.push ...
10:40
Adding to Arrays: Javascript and NodeJS for Complete Beginners ...
07:38
5 Ways to Add Items to Arrays in JavaScript - YouTube
Designcise
designcise.com › web › tutorial › how-to-add-array-values-to-an-existing-javascript-set
How to Add Array Values to an Existing JavaScript Set? - Designcise
September 27, 2021 - // ES6+ const arr = [1, 2, 3, 3, 4]; const existingSet = new Set(); existingSet.add(1); existingSet.add(2); existingSet.add(5); arr.forEach((item) => existingSet.add(item)); console.log(existingSet); // Set(5) { 1, 2, 5, 3, 4 } You can of course, use any other loop to iterate over the array as you want.
MDN 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.
Mastering JS
masteringjs.io › tutorials › fundamentals › add-to-array
Add an Element to an Array in JavaScript - Mastering JS
June 18, 2021 - Using includes() works, but can cause performance issues because includes() scans through the entire array every time you call it. So the below loop is O(n^2). const arrWithoutDuplicates = []; for (let i = 0; i < arr.length; ++i) { if (arrWithoutDuplicates.includes(arr[i])) { continue; } arrWithoutDuplicates.push(arr[i]); } Instead, we recommend using a JavaScript set to represent a collection of objects where every element should be unique. const set = new Set(['a', 'b', 'c']); set.add('a'); set; // Set(3) { 'a', 'b', 'c' } set.add('d'); set; // Set(4) { 'a', 'b', 'c', 'd' }
Mimo
mimo.org › tutorials › javascript › how-to-add-to-an-array-in-javascript
How to Add to an Array in JavaScript
Learn how to add to an array in JavaScript using push(), unshift(), splice(), and spread for mutable or immutable updates.
freeCodeCamp
freecodecamp.org › news › set-and-map-in-javascript
How to Use Set and Map in JavaScript
February 10, 2023 - And it returns a value, which was an array of a single object that contains a date and price. ... We added the key-value pair kamal and lawal to an empty map with the set method. The set method is where you set a key-value pair. The key has to be the first option and the value has to be the last.
JSchallenger
jschallenger.com › javascript-practice › javascript-sets › add-multiple-elements-to-javascript-set
Add multiple elements to Set | JSchallenger
JSchallenger. Write a function that takes a Set and an array as arguments. If not already existing, add each element in the array to the Set. Return the modified Set
Scaler
scaler.com › home › topics › how to convert set to array in javascript
How to Convert Set to Array in JavaScript - Scaler Topics
May 4, 2023 - JavaScript offers several ways to convert a set to an array but forEach() method is more efficient because every element in the array is given a separate execution of the provided function by the forEach method.
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › TypedArray › set
TypedArray.prototype.set() - JavaScript - MDN Web Docs
July 10, 2025 - const buffer = new ArrayBuffer(8); const uint8 = new Uint8Array(buffer); uint8.set([1, 2, 3], 3); console.log(uint8); // Uint8Array [ 0, 0, 0, 1, 2, 3, 0, 0 ] Polyfill of TypedArray.prototype.set in core-js · JavaScript typed arrays guide · TypedArray · ArrayBuffer ·
GeeksforGeeks
geeksforgeeks.org › how-to-convert-array-to-set-in-javascript
How to Convert Array to Set in JavaScript? | GeeksforGeeks
October 25, 2024 - Converting Array to Set means transforming the array elements into a Set object. The Set constructor in JavaScript directly converts an array into a Set. This process automatically filters out duplicate elements, creating a collection of unique ...
GeeksforGeeks
geeksforgeeks.org › javascript › set-vs-array-in-javascript
Set vs Array in JavaScript - GeeksforGeeks
August 5, 2025 - let sample = new Set(); sample.add("Hello"); sample.add(1) sample.add("Bye") sample.add("@"); for (let item of sample) { console.log(item); } ... The array is a data structure that is used to store data in a sequential manner of the same type.