JavaScript Set Methods

What is JavaScript Set Methods?

JavaScript Sets are collections of unique values and they come equipped with several built-in methods to perform common operations. These methods make it easy to add, remove and check elements, among other tasks.

List of JavaScript Set Methods

  1. add()
  2. delete()
  3. has()
  4. clear()
  5. size (property, not a method)
  6. forEach()
  7. values()
  8. keys()
  9. entries()

1. add() Method

The add() method adds a new element to the Set. If the element already exists, it is ignored because Sets only store unique values.

Syntax:

set.add(value);

Example:

const mySet = new Set();
mySet.add('apple');
mySet.add('banana');
mySet.add('apple'); // Duplicate, ignored

console.log(mySet); // Output: Set(2) { 'apple', 'banana' }

2. delete() Method

The delete() method removes a specific element from the Set. It returns true if the element was removed and false if the element was not found.

Syntax:

set.delete(value);

Example:

const mySet = new Set(['apple', 'banana']);
console.log(mySet.delete('apple')); // Output: true
console.log(mySet.delete('orange')); // Output: false
console.log(mySet); // Output: Set(1) { 'banana' }

3. has() Method

The has() method checks if a specific element exists in the Set. It returns true if the element is found,and false otherwise.

Syntax:

set.has(value);

Example:

const mySet = new Set(['apple', 'banana']);
console.log(mySet.has('apple')); // Output: true
console.log(mySet.has('orange')); // Output: false

4. clear() Method

The clear() method removes all elements from the Set, leaving it empty.

Syntax:

set.clear();

Example:

const mySet = new Set(['apple', 'banana', 'orange']);
mySet.clear();

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

5. size Property

The size property returns the number of elements in a Set.

Syntax:

set.size;

Example:

const mySet = new Set(['apple', 'banana', 'orange']);
console.log(mySet.size); // Output: 3

6. forEach() Method

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

Syntax:

set.forEach(callbackFunction);

Example:

const mySet = new Set(['apple', 'banana', 'orange']);
mySet.forEach((value) => {
console.log(value);
});
// Output:
// apple
// banana
// orange

7. values() Method

The values() method returns an iterator object that contains all the values of the Set.

Syntax:

set.values();

Example:

const mySet = new Set(['apple', 'banana', 'orange']);
const values = mySet.values();

for (const value of values) {
console.log(value);
}
// Output:
// apple
// banana
// orange

8. keys() Method

The keys() method is identical to values() in Sets. It returns an iterator containing all the values (since Sets do not have keys).

Syntax:

set.keys();

Example:

const mySet = new Set(['apple', 'banana', 'orange']);
for (const key of mySet.keys()) {
console.log(key);
}
// Output:
// apple
// banana
// orange

9. entries() Method

The entries() method returns an iterator containing [value, value] pairs for each element in the Set. This format mimics the behavior of Map objects.

Syntax:

set.entries();

Example:

const mySet = new Set(['apple', 'banana', 'orange']);
for (const entry of mySet.entries()) {
console.log(entry);
}
// Output:
// [ 'apple', 'apple' ]
// [ 'banana', 'banana' ]
// [ 'orange', 'orange' ]

Practical Applications of Set Methods

Removing Duplicates from an Array

const duplicates = [1, 2, 2, 3, 4, 4];
const uniqueArray = [...new Set(duplicates)];

console.log(uniqueArray); // Output: [1, 2, 3, 4]

Checking Membership

const allowedUsers = new Set(['Alice', 'Bob', 'Charlie']);
console.log(allowedUsers.has('Alice')); // Output: true
console.log(allowedUsers.has('Eve')); // Output: false

Efficient Filtering

const setA = new Set([1, 2, 3, 4]);
const setB = new Set([3, 4, 5, 6]);

// Find common elements (intersection)
const intersection = new Set([...setA].filter(x => setB.has(x)));
console.log(intersection); // Output: Set(2) { 3, 4 }

Leave a Comment