JS: Set Constructor
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' }