Skip to main content

JavaScript Sets and Sets Methods

By SamK
0
0 recommends
Category(s)
Topic(s)

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.

You can create a JavaScript Set in two ways:

  • Passing an array to new Set()
  • Creating an empty Set and adding values using .add()

Creating a Set Using an Array

// Create a Set from an array
const letters = new Set(["a", "b", "c"]);

console.log(letters);  
// Output: Set { 'a', 'b', 'c' }

Creating an Empty Set and Adding Values

// Create an empty Set
const letters = new Set();

// Add values to the Set
letters.add("a");
letters.add("b");
letters.add("c");

console.log(letters);  
// Output: Set { 'a', 'b', 'c' }

Adding Variables to a Set

// 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);

console.log(letters);  
// Output: Set { 'a', 'b', 'c' }

Properties and Methods

The .add() Method

The .add() method is used to add new elements to a Set.

const letters = new Set(["a", "b", "c"]);
letters.add("d");
letters.add("e");

console.log(letters);  
// Output: Set { 'a', 'b', 'c', 'd', 'e' }

If you try to add duplicate elements, only the first occurrence will be stored:

letters.add("c");
letters.add("c");
letters.add("c");

console.log(letters);  
// Output: Set { 'a', 'b', 'c', 'd', 'e' }

The .delete() Method

Removes a specific value from a Set.

const numbers = new Set([1, 2, 3, 4]);
numbers.delete(3);

console.log(numbers);  
// Output: Set { 1, 2, 4 }

The .has() Method

Checks if a value exists in a Set.

const items = new Set(["apple", "banana", "orange"]);

console.log(items.has("banana"));  // Output: true
console.log(items.has("grape"));   // Output: false

The .size Property

Returns the number of elements in a Set.

const numbers = new Set([10, 20, 30, 40]);
console.log(numbers.size);  // Output: 4

The .clear() Method

Removes all elements from a Set.

const items = new Set(["pen", "book", "eraser"]);
items.clear();

console.log(items);  
// Output: Set {}

Checking Set Type

typeof Operator

The typeof operator returns "object" for Sets.

const letters = new Set(["x", "y", "z"]);
console.log(typeof letters);  
// Output: 'object'

instanceof Operator

The instanceof operator checks if an object is an instance of Set.

console.log(letters instanceof Set);  // Output: true

The forEach() Method

The forEach() method executes a function for each element in the Set.

const letters = new Set(["a", "b", "c"]);
let text = "";
letters.forEach((value) => {
 text += value + " ";
});

console.log(text.trim()); // Output: "a b c"

The values() Method

The values() method returns an iterator containing the values of the Set.

const letters = new Set(["a", "b", "c"]);
const myIterator = letters.values();
for (const entry of myIterator) {
 console.log(entry);
}

/* Output:
"a"
"b"
"c" */

The keys() Method

The keys() method returns an iterator containing the values of the Set. Since Set does not have keys, keys() behaves the same as values().

const letters = new Set(["a", "b", "c"]);
const myIterator = letters.keys();
for (const x of myIterator) {
 console.log(x);
}

/* Output:
"a"
"b"
"c" */

The entries() Method

The entries() method returns an iterator with [value, value] pairs from a Set. This method is provided to maintain compatibility with Map.

const letters = new Set(["a", "b", "c"]);
const myIterator = letters.entries();
for (const entry of myIterator) {
 console.log(entry);
}

/* Output:
["a", "a"]
["b", "b"]
["c", "c"] */

Questions & Answers