Skip to main content

Archive

Show more

JavaScript Set Methods

JavaScript Set Methods

The Set object in JavaScript allows you to store unique values of any type, whether primitive values or object references. It automatically handles duplicates, ensuring each value in the set is unique. This article covers some of the essential methods available for the Set object.


Common Set Methods

Here are some commonly used methods of the Set object:

// Create a new Set
const set = new Set();

// Adding values
set.add('apple');
set.add('banana');
set.add('apple'); // Duplicate value will not be added

// Method: has
console.log(set.has('apple')); // Output: true
console.log(set.has('grape')); // Output: false

// Method: delete
set.delete('banana');
console.log(set.has('banana')); // Output: false

// Method: clear
set.clear();
console.log(set.size); // Output: 0

Set Methods Explained

add

The add method adds a new value to the set. If the value already exists, it will not be added again.

const set = new Set();
set.add('value1');
console.log(set.has('value1')); // Output: true
set.add('value1'); // Duplicate value will not be added
console.log(set.size); // Output: 1

has

The has method checks if a value exists in the set. It returns true if the value is present, otherwise false.

const set = new Set();
set.add('value1');
console.log(set.has('value1')); // Output: true
console.log(set.has('value2')); // Output: false

delete

The delete method removes a value from the set. It returns true if the value was successfully removed, otherwise false.

const set = new Set();
set.add('value1');
set.delete('value1');
console.log(set.has('value1')); // Output: false

clear

The clear method removes all values from the set, leaving it empty.

const set = new Set();
set.add('value1');
set.add('value2');
set.clear();
console.log(set.size); // Output: 0

keys

The keys method returns an iterator object that contains the values of the set. Since the values in a set are unique and unordered, the iterator will iterate over the values in insertion order.

const set = new Set();
set.add('value1');
set.add('value2');
for (let key of set.keys()) {
  console.log(key);
}
// Output:
// value1
// value2

values

The values method returns an iterator object that contains the values of the set. It is identical to the keys method for sets.

const set = new Set();
set.add('value1');
set.add('value2');
for (let value of set.values()) {
  console.log(value);
}
// Output:
// value1
// value2

entries

The entries method returns an iterator object that contains an array of [value, value] pairs for each value in the set. This method is provided for compatibility with the Map object.

const set = new Set();
set.add('value1');
set.add('value2');
for (let [value, valueAgain] of set.entries()) {
  console.log(value, valueAgain);
}
// Output:
// value1 value1
// value2 value2

forEach

The forEach method executes a provided function once for each value in the set, in insertion order.

const set = new Set();
set.add('value1');
set.add('value2');
set.forEach((value) => {
  console.log(value);
});
// Output:
// value1
// value2

Conclusion

JavaScript's Set object provides an efficient way to manage a collection of unique values. Understanding and utilizing the various methods of the Set object will help you effectively handle and manipulate sets in your JavaScript applications, leading to cleaner and more efficient code.

Comments