JS TutorialLesson #74

JavaScript Sets

A JavaScript Set is a collection of unique values.

A JavaScript Set is a collection of unique values.

Each value can only occur once in a Set.

The values can be of any type, primitive values or objects.


Share

How to Create a Set

You can create a JavaScript Set by:

  • Passing an array to new Set()
  • Create an empty set and use add() to add values

The new Set() Method

Pass an array to the new Set() constructor:

Example

Example Code

javascript
Try it Yourself
// Create a Set

const letters = new Set(["a","b","c"]);

Create a Set and add values:

Example

Example Code

javascript
Try it Yourself
// Create a Set

const letters = new Set();

// Add Values to the Set

letters.add("a");

letters.add("b");

letters.add("c");

Create a Set and add variables:

Example

Example Code

javascript
Try it Yourself
// Create a Set

const letters = new Set();

// Create Variables

const a = "a";

const b = "b";

const c = "c";

// Add Variables to the Set

letters.add(a);

letters.add(b);

letters.add(c);

The add() Method

Example

Example Code

javascript
Try it Yourself
letters.add("d");

letters.add("e");

If you add equal elements, only the first will be saved:

Example

Example Code

javascript
Try it Yourself
letters.add("a");

letters.add("b");

letters.add("c");

letters.add("c");

letters.add("c");

letters.add("c");

letters.add("c");

letters.add("c");

Listing the Elements

You can list all Set elements (values) with a for..of loop:

Example

Example Code

javascript
Try it Yourself
// Create a Set

const letters = new Set(["a","b","c"]);

// List all Elements

let text = "";

for (const x of letters) {

  text += x;

}

Sets are Objects

Example

Example Code

javascript
Try it Yourself
typeof letters;      // Returns object

Learn More: > JavaScript Set Methods > JavaScript Set Logic > JavaScript Weak Sets > JavaScript Set Reference > JavaScript Maps

Browser Support

Set is an ES6 feature.

ES6 is fully supported in all modern browsers since June 2017:

Chrome 51Edge 15Firefox 54Safari 10Opera 38
May 2016Apr 2017Jun 2017Sep 2016Jun 2016


Interactive Code Examples

JS Example 1

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
// Create a Set

const letters = new Set(["a","b","c"]);

JS Example 2

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
// Create a Set

const letters = new Set();



// Add Values to the Set

letters.add("a");

letters.add("b");

letters.add("c");

JS Example 3

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
// Create a Set

const letters = new Set();



// Create Variables

const a = "a";

const b = "b";

const c = "c";



// Add Variables to the Set

letters.add(a);

letters.add(b);

letters.add(c);

JS Example 4

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
letters.add("d");

letters.add("e");

JS Example 5

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
letters.add("a");

letters.add("b");

letters.add("c");

letters.add("c");

letters.add("c");

letters.add("c");

letters.add("c");

letters.add("c");

JS Example 6

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
// Create a Set

const letters = new Set(["a","b","c"]);



// List all Elements

let text = "";

for (const x of letters) {

  text += x;

}