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
🌐
W3Schools
w3schools.com › js › js_sets.asp
JavaScript Sets
The values can be of any type, primitive values or objects. Share Link Copied · You can create a JavaScript Set by: Passing an array to new Set() Create an empty set and use add() to add values ·
🌐
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).
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript-set-add-method
JavaScript Set add() Method | GeeksforGeeks
July 12, 2024 - The Set.add() method in JavaScript is used to append an element with a specified value in a set. It modifies the existing set by appending the new element but does not return a new set.
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-convert-set-to-array-in-javascript
Set to Array in JS or JavaScript | GeeksforGeeks
May 10, 2025 - Strings in JavaScript are immutable (cannot be changed directly). However, arrays are mutable, allowing you to perform operations such as adding, removing, or modifying elements.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-convert-array-to-set-in-javascript
How to Convert Array to Set in JavaScript? - GeeksforGeeks
July 12, 2025 - The Set constructor is used to convert the array into set in JavaScript. It is useful to clean the array and remove duplicate elements.
🌐
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()):
🌐
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).
🌐
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] js ·
🌐
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 - All of the elements that the Set contains were included into a new array using the spread operator. ... A different approach is to manually add each Set component to the array.
🌐
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' }
🌐
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 - This post will discuss how to convert a Set to an array in JavaScript... The `Array.from()` method can be used to create shallow-copied `Array` instances from iterable objects such as Set. It was added to the Array object with ES6.
🌐
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
🌐
freeCodeCamp
freecodecamp.org › news › set-and-map-in-javascript
How to Use Set and Map in JavaScript
February 10, 2023 - An iterable such as an array, set, or nodelist can be passed to the set. 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 in the set.
🌐
Medium
medium.com › @conboys111 › how-to-easily-convert-a-set-to-an-array-and-vice-versa-in-javascript-a4e28ac9ccef
How to Easily Convert a Set to an Array and Vice Versa in JavaScript | by myHotTake | Medium
November 4, 2024 - This is the flexibility of arrays — they don’t care about uniqueness, just the order of things. On the other hand, if I want to ensure that no duplicate artifacts are ever allowed in my collection, I can always use a Set. Let’s say I find some new artifacts but don’t want to allow duplicates: // Adding new artifacts to the display case, ensuring no duplicates displayCase.add("Painting"); displayCase.add("Coin"); // "Coin" already exists, so it won't be added again console.log(displayCase); // Set { "Coin", "Vase", "Sculpture", "Painting" }
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]));