JS: Set Constructor

By Xah Lee. Date: .

New in ES2015.

new Set() → creates a empty set.

new Set(iterable) → create a set from values in iterable object iterable.

[see JS: 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' }

JS Set

  1. Set Tutorial
  2. Union, Intersection, Difference

Like it? Help me by telling your friends. Or, Put $5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put $5 at patreon and message me.

Web Dev Tutorials

  1. HTML
  2. CSS
  3. JavaScript
  4. JS Obj Ref
  5. DOM
  6. SVG
  7. Blog