Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Set literal in JavaScript?

Tags:

I can make a Set with new Set(), same way I can use the Array or Object or Boolean or Number constructors.

But is there a set literal syntax, like there is for arrays, objects, booleans, numbers etc?

like image 564
mikemaccana Avatar asked Feb 12 '16 17:02

mikemaccana


2 Answers

As others have pointed out, there is no Set (or Map) literal syntax yet. There have been a few ideas floating around, in this ES Discuss Thread and in the following twitter discussion.

A few proposed syntax examples:

const set = {<1, "two", false>}; // by Brendan Eich
const set = {. 1, "two", false .}; // by Axel Rauschmayer

There are, as far as I can see, no proposals to implement any of them yet, though.

like image 131
nils Avatar answered Oct 06 '22 15:10

nils


Well, there's no literal syntax for Set() but you can use an array instead. They are both very similar and can easily be switched between from using the functions:

Array.from(mySet) // Converts mySet into an array
new Set(myArray) // Creates a set from myArray
like image 33
Reda Avatar answered Oct 06 '22 15:10

Reda