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
🌐
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); const newSet = new Set([ ...existingSet, ...arr ]); console.log(newSet); // Set(5) { 1, 2, 5, 3, 4 } This works because ...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript-set-add-method
JavaScript Set add() Method | GeeksforGeeks
July 12, 2024 - The Set.add() method does not modify the set if the element with a specified value already exists in the set. ... Example 1: In this example, we have added two numbers 23 and 12 into the set.
🌐
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. Each value can only occur once in a Set. The values can be of any type, primitive values or objects. ... // Create a Set const letters = new Set(); // Add Values to the Set letters.add("a"); letters.add("b"); letters.add("c"); Try it Yourself »
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Set › add
Set.prototype.add() - JavaScript - MDN Web Docs - Mozilla
const mySet = new Set(); mySet.add(1); mySet.add(5).add("some text"); // chainable console.log(mySet); // Set [1, 5, "some text"]
🌐
Medium
medium.com › @conboys111 › what-happens-when-you-add-objects-or-arrays-to-a-javascript-set-b9f15dbbc5e0
What Happens When You Add Objects or Arrays to a JavaScript Set? | by myHotTake | Medium
November 11, 2024 - Here, since the same object (dragon1) is being added twice, the Set recognizes it and rejects the duplicate. It’s like someone trying to sneak the exact same dragon back into the shelter—nope, I already know that dragon! Arrays in JavaScript behave similarly to objects in Sets.
🌐
Techie Delight
techiedelight.com › home › java › insert all array elements into a set in javascript
Insert all array elements into a set in JavaScript | Techie Delight
July 7, 2026 - Here’s an example: ... We can use the Array.forEach() function to execute a function for each element of the array and use the Set.add() function inside the function to add each element to the set.
🌐
GoLinuxCloud
golinuxcloud.com › home › programming › javascript set add(): add values to a set
How to use JavaScript Set add() Method? [SOLVED]
June 17, 2026 - No. If the Set already contains the value, adding it again does not create a duplicate. Use chained add() calls, loop over an array, or create the Set from an array: new Set(array).
Find elsewhere
🌐
Mastering JS
masteringjs.io › tutorials › fundamentals › add-to-array
Add an Element to an Array in JavaScript - Mastering JS
June 18, 2021 - So the below loop is O(n^2). const ... 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' }...
🌐
DEV Community
dev.to › clairecodes › how-to-create-an-array-of-unique-values-in-javascript-using-sets-5dg6
How to create an array of unique values in JavaScript using Sets - DEV Community
May 8, 2018 - There are two ways to add values to a Set. Firstly by using the add method as above. Secondly by passing an array to the constructor (new Set()):
🌐
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
🌐
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 - // Create an ArrayBuffer with a size in bytes const buffer = new ArrayBuffer(8); const uint8 = new Uint8Array(buffer); // Copy the values into the array starting at index 3 uint8.set([1, 2, 3], 3); console.log(uint8); // Expected output: Uint8Array [0, 0, 0, 1, 2, 3, 0, 0]
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-convert-array-to-set
How to Convert an Array to a Set in JavaScript | bobbyhadz
Set objects and arrays are iterable, so we are able to use the spread syntax (...) to unpack them into a new Set. You can also use a for...of loop to add an array of values to an existing Set.
🌐
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. Set objects are collections of values. A value in the set may only occur once; it is unique in the set's collection. 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).
🌐
freeCodeCamp
freecodecamp.org › news › set-and-map-in-javascript
How to Use Set and Map in JavaScript
February 10, 2023 - The example below shows a set created from an array of 4 elements. ... From the result, the code returns a set with 4 elements. Sets have several methods and a properties that you can use to retrieve, add, delete, check, and clear all the elements ...
🌐
Techie Delight
techiedelight.com › home › java › convert a set to an array in javascript
Convert a Set to an array in JavaScript | Techie Delight
July 7, 2026 - Another solution is to individually add each element in the set to the array. This can be easily done using the forEach() function. This function iterates over the elements of an iterable object, such as a set, and pushes them to a new array. Here’s an example of this approach:
🌐
Medium
habtesoft.medium.com › add-elements-to-an-array-in-javascript-a9cc6cd9469f
Add elements to an array in JavaScript | by habtesoft | Medium
October 18, 2024 - The splice() method can also be used to add elements to an array in JavaScript. The splice() method adds or removes elements from an array at a specified index. Here’s an example of how to add elements to an array using splice():
🌐
Mimo
mimo.org › tutorials › javascript › how-to-add-to-an-array-in-javascript
How to Add to an Array in JavaScript
Use unshift() to add items to the start of an array. ... It shifts existing elements to the right. Use splice() when you need to insert at a certain index. ... First argument: index to start. Second argument: how many items to remove. Remaining arguments: items to insert.
Top answer
1 of 5
36

No there is not.

A Set works on objects and primitives and is useful for preventing identical primitives and re-adding the same object instance.

Each array is their own object, so you can actually add two different arrays with the same values.

var set = new Set();
set.add([3, 4]);
set.add([3, 4]);
console.log(set.size);//2

Additionally, there's nothing to prevent an object from being changed once in a set.

var set = new Set();
var a1 = [3, 4];
var a2 = [3, 4];
set.add(a1);
set.add(a2);
a2.push(5);
for (let a of set) {
    console.log(a);
}
//Outputs:
// [3, 4]
// [3, 4, 5]

A set does not have a mechanism for checking the values of objects in a set. Since the value of an object could change at any time, it wouldn't be much more efficient than simply looping over them yourself.

The functionality you are looking for has been kicked around in various ECMAScript proposals:

  • Composites

  • Record and Tuple (withdrawn at stage 2)

It does not appear to be coming anytime soon.

2 of 5
8

It's not using a Set, but may solve your problem.

If you want to make sure that an array can only exist in a collection once, and you want instant lookups you can use a hash by abusing its keys. They do not necessarily need a value. You can set them to undefined, to true, or something else if it makes you sleep better at night 😛.

Inspired by: javascript search array of arrays

It is halfway tempting to make a wrapper, but it is also simple enough to handle a small job.

const hash = {};

hash[[1, 2, 3]] = undefined;
hash[[3, 4]] = undefined;
hash[[3, 4]] = undefined;

console.log({hash});
console.log("hash has [3, 4]?", hash.hasOwnProperty([3, 4]));

For me, this will work fine, because I am checking if a coordinate exists in a collection. Unfortunately from this stackoverflow it seems that Set cannot be the answer to this problem.

Coincidentally it seems to implicitly solve your problem of not calling toString on the array which this seems to do under the hood. Personally I don't care for hash.hasOwnProperty.

A More Elegant Option

You could even write a wrapper to your code like:

class ArraySet extends Set {
  add(arr) {
    super.add(arr.toString());
  }
  has(arr) {
    return super.has(arr.toString());
  }
}

const arraySet = new ArraySet();

arraySet.add([1, 2]);
arraySet.add([3, 4]);
arraySet.add([3, 4]);

console.log("ArraySet has [3, 4]?", arraySet.has([3, 4]));