JavaScript Set Objects: When and How to Use Them | by Tanvir Hasan Prince

JavaScript Set Objects: When and How to Use Them | by Tanvir Hasan Prince

In JavaScript, a Set is a built-in object that lets you store unique values of any type, whether primitive values or object references. Unlike arrays, sets do not allow duplicate elements, meaning each value in a set is unique.

Key Features of a Set

  1. Uniqueness: Each value in a Set can occur only once. This is useful for storing collections of unique elements.
  2. Value Types: Values can be of any type, including primitive types like numbers and strings, and complex types like objects and arrays.
  3. Order of Elements: Elements in a Set are ordered based on insertion order. This means that when you iterate over the elements, they will appear in the order they were added.

Common Methods and Properties

  • add(value): Adds a new element with the given value to the set. If the value already exists, it will not be added again.
  • delete(value): Removes an element with the specified value from the set.
  • has(value): Returns a boolean indicating whether an element with the specified value exists in the set.
  • clear(): Removes all elements from the set.
  • size: Returns the number of elements in the set.

Example Usage

Here’s a simple example to demonstrate how a Set works in JavaScript:

// Creating a new Set let mySet = new Set();  // Adding values to the Set mySet.add(1); mySet.add(2); mySet.add(3); mySet.add(3); // Duplicate value, will not be added console.log(mySet.size); // Output: 3  // Checking if a value exists in the Set console.log(mySet.has(2)); // Output: true console.log(mySet.has(4)); // Output: false  // Deleting a value from the Set mySet.delete(2); console.log(mySet.has(2)); // Output: false  // Iterating over the Set mySet.forEach(value => {   console.log(value); }); // Output: // 1 // 3  // Clearing all elements from the Set mySet.clear(); console.log(mySet.size); // Output: 0

Use Cases for Sets

  • Removing Duplicates: Sets are often used to eliminate duplicate values from an array or collection.
  • Efficient Lookups: Sets provide efficient checks for the existence of an element, which is useful in scenarios where you need to frequently check for membership.
  • Unique Collections: When you need to maintain a collection of unique items, such as unique tags or identifiers, sets are a natural fit.

Comparison with Arrays

  • Duplicates: Arrays can contain duplicate elements, whereas sets automatically filter out duplicates.
  • Order: Arrays maintain the order of elements as they are added. Sets also maintain insertion order but do not allow reordering.
  • Performance: Sets have faster look-up times for checking the existence of an element compared to arrays.

Using Set is particularly advantageous when you need to handle collections of unique values and perform operations like membership checking and deduplication efficiently.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top