JS: Set Constructor

By Xah Lee. Date: . Last updated: .

New in JS2015.

new Set()
Create a empty set.
new Set(iterable)
Create a set from values in Iterable Object iterable.
const s = new Set([3,4,5]);
console.log(s);
// Set { 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 s = new Set([3, 4, 5]);
console.log(s); // Set { '3', '4', '5' }

Example of converting a string to a set:

// string to set of chars
const t = new Set("cat 😞");
t.add ("6");
console.log(t); // Set { 'c', 'a', 't', ' ', '😞', '6' }

JavaScript, Set Object

BUY ΣJS JavaScript in Depth