JS: Set Constructor
(new in JS: ECMAScript 2015)
new Set()
-
Create a empty set.
new Set(iterable)
-
Create a set from values in Iterable Object iterable.
const xx = new Set([3, 4, 5]) console.log(xx) // Set(3) { 3, 4, 5 }
Literal Expression for Set
There's no literal expression for set. But you can pass in a literal expression of array.
// using array as literal expression for set const xx = new Set([3, 4, 5]) console.log(xx) // Set(3) { 3, 4, 5 }
Example. Convert a string to a set
// string to set of chars const xx = new Set("cat 😸") xx.add("6") console.log(xx) // Set(6) { "c", "a", "t", " ", "😸", "6" }