Just pass the array to the Set constructor. The Set constructor accepts an iterable parameter. The Array object implements the iterable protocol, so its a valid parameter.

var arr = [55, 44, 65];
var set = new Set(arr);
console.log(set.size === arr.length);
console.log(set.has(65));
Run code snippetEdit code snippet Hide Results Copy to answer Expand

See here

Answer from levi on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › from
Array.from() - JavaScript | MDN
The Array.from() static method creates a new, shallow-copied Array instance from an iterable or array-like object.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Set
Set - JavaScript | MDN
August 13, 2026 - This is to make maps valid set-like objects, because for maps, the iterator produces entries but the has() method takes keys. Arrays are not set-like because they don't have a has() method or the size property, and their keys() method produces indices instead of elements.
🌐
W3Schools
w3schools.com › js › js_sets.asp
JavaScript Sets
You can create a JavaScript Set by: Passing an array to new Set() Create an empty set and use add() to add values · Pass an array to the new Set() constructor: // Create a Set const letters = new Set(["a","b","c"]); Try it Yourself » · Create ...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-convert-set-to-array-in-javascript
Set to Array in JS or JavaScript - GeeksforGeeks
July 11, 2025 - The JavaScript Spread Operator can be used to destructure the elements of the array and then assign them to a new variable in the form of an array. ... JavaScript Array.from() Method returns a new Array from an array, object or other iterable ...
Find elsewhere
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › TypedArray › set
TypedArray.prototype.set() - JavaScript | MDN
July 10, 2025 - The set() method of TypedArray instances stores multiple values in the typed array, reading input values from a specified array.
🌐
Medium
medium.com › front-end-weekly › es6-set-vs-array-what-and-when-efc055655e1a
ES6 — Set vs Array — What and when? | by Maya Shavin | Frontend Weekly | Medium
November 9, 2018 - You can also subscribe to our weekly newsletter at http://frontendweekly.co ... Everyone who works with JS until now is familiar with Array (don’t tell me you don’t). But what exactly is Array? Well, in general, Array is type of structure representing block of data (numbers, objects, etc…) allocated in consecutive memory. ... Set, more familiar as a Math concept, is an abstract data type which contains only distinct elements/objects without the need of being allocated orderly by index.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › Array
Array() constructor - JavaScript | MDN
1 week ago - Arrays can be created using a constructor with a single number parameter. An array is created with its length property set to that number, and the array elements are empty slots.
🌐
W3Schools
w3schools.com › js › js_arrays.asp
JavaScript Arrays
You should use arrays when you want the element names to be numbers. JavaScript has a built-in array constructor new Array().
🌐
Latenode
community.latenode.com › other questions › javascript
How can I turn a Set into an Array? - JavaScript - Latenode Official Community
October 11, 2024 - The spread operator takes the iterable (in this case, the Set) and expands its elements into a new array. This method is both efficient and widely supported across modern JavaScript environments. For those looking for an alternative to the spread operator, the Array.from method provides another straightforward solution:
🌐
DEV Community
dev.to › keevcodes › the-javascript-set-object-2f75
The JavaScript Set Object - DEV Community
June 27, 2019 - Wait... what??? That's awesome! Yeah, my fellow developer, yeah. Instead of using the filter method, all we have to do is pass our postTypes array into the Set object and wrap it into an array.
🌐
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 - So, back to our analogy, the storage room (Array) is great when I care about order or want to allow duplicates, like tracking every artifact that arrives. Meanwhile, the display case (Set) is perfect when I want to guarantee that every artifact is unique and don’t care about the order they came in. JavaScript gives me tools to easily switch between the two, depending on my needs — whether that’s using Array.from() or the spread operator to go from Set to Array, or just using new Set() to clean up duplicates from an Array.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-convert-array-to-set-in-javascript
How to Convert Array to Set in JavaScript? - GeeksforGeeks
July 12, 2025 - 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 ...
🌐
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.
🌐
Koladechris
koladechris.com › blog › what-are-the-differences-between-array-and-set-in-javascript
What are the Differences between Array and Set in JavaScript?
December 1, 2024 - An Array allows direct indexing by position, but a set does not. If you want to access an item in a set by positioning, you have to convert it to an array. In the array below, you can access any of the elements by specify its index: ... JavaScript has unique built-in methods for manipulating both arrays and sets.
Top answer
1 of 2
6

The use of either Array or Set by your function is equivalent: You create a container, and you push stuff into it. This leaves two areas for consideration:

  1. What does the consumer need to do with the container?

  2. Which container is more efficient (space or time)?

If you find that the consumer just iterates over the container, both Array and Set are equivalent again from this usage perspective. However, if your user needs to answer the question "is this index in the container?", then Set is the only appropriate container type.

Once you have asserted that the container type is indeed irrelevant to use, both on the producer and consumer side, go for the more efficient container:

  • Array should be implemented as a simple slap of contiguous memory that holds (the references to) its elements. A push() generally means 1) checking for space (one comparison), 2) appending the new element to the end of the buffer, and 3) updating the length field.

  • Set is required to ensure that its elements are unique. That is, where Array.push() is happy to push an element twice, Set.insert() must somehow catch this condition and handle it. This requires extra work. To get O(1) performance, Set would normally be implemented as a hash table. Hash tables are fast, but they are definitely more complex than a simple, contiguous buffer. Thus, Set.insert() will generally have to do significantly more work than Array.push(), and its data structures are very likely to consume more memory.

Both space and time efficiency are better for Array than for Set. And this is why I would default to using Array over Set whenever their use is equivalent.

2 of 2
2

From what you have shown in your question, it does not matter (and consider to refactor your code in case it starts to matter).

not limited to this case, if I found both Array and Set can finish my task, should I use Array or Set

Producing an Array or a Set is typically not an end in itself, it is a means to an end, so often you can find out what the caller of your method which produces this result might prefer.

Note, if you start using an array, and it turns out for further processing a Set would be beneficial (or you need both, the array and the set representation), you can easily convert it:

  let resultSet = new Set(results)

I am not a Javascript expert, but I would expect the conversion the other way round to require at a little bit more code. Thus, if in doubt, I personally would start with an array first, and switch to a set if it fits better, but that is somewhat opinionated.

🌐
Just Academy
justacademy.co › blog-detail › how-to-convert-set-to-array-in-javascript
How To Convert Set To Array In JavaScript by Roshan Chaturvedi | JustAcademy
April 23, 2024 - When converting a set to an array, you can easily manipulate the data as arrays offer more flexibility in terms of accessing, modifying, and iterating over the elements. Additionally, arrays have built-in methods like map, filter, and reduce that can be directly applied to the converted array, making data manipulation more efficient and simpler.
🌐
GitHub
github.com › lodash › lodash › issues › 460
Method for array to "set" · Issue #460 · lodash/lodash
January 23, 2014 - Hey John. Is there a way to convert an array to a "set" of sorts -- basically an object keyed by the array elements, with all values set to true? For example, something like this: _.toSet( [ 'a', 'b', 'c' ] ); // produces { a: true, b: t...
Author: lodash