Set Operations

Introduction

After creating a Set, you can perform various operations to add, remove, search, and iterate through its values. JavaScript provides several built-in methods that make working with Sets simple and efficient.

Unlike arrays, Sets store only unique values, so these operations are optimized for managing collections where duplicates are not allowed.

Set operations are commonly used in web development, Node.js applications, API testing, and automation frameworks such as Selenium, Playwright, and Cypress.

For automation engineers, Set operations are useful for managing unique test users, browser names, API endpoints, execution IDs, environment names, and duplicate-free test data.


Common Set Operations

JavaScript provides the following commonly used Set methods:

MethodDescription
add()Adds a value to the Set.
delete()Removes a value from the Set.
has()Checks whether a value exists in the Set.
clear()Removes all values from the Set.
sizeReturns the number of values in the Set.

Adding Values (add())

The add() method inserts a new value into a Set. If the value already exists, it is ignored.

Syntax

setName.add(value);

Example

let fruits = new Set();

fruits.add("Apple");

fruits.add("Mango");

fruits.add("Orange");

console.log(fruits);

Output

Set(3) { 'Apple', 'Mango', 'Orange' }

Duplicate Values Are Ignored

let numbers = new Set();

numbers.add(10);

numbers.add(20);

numbers.add(10);

console.log(numbers);

Output

Set(2) { 10, 20 }

Removing Values (delete())

The delete() method removes a specific value from the Set.

Syntax

setName.delete(value);

Example

let colors = new Set([

    "Red",

    "Green",

    "Blue"

]);

colors.delete("Green");

console.log(colors);

Output

Set(2) { 'Red', 'Blue' }

Checking if a Value Exists (has())

The has() method returns true if a value exists; otherwise, it returns false.

Syntax

setName.has(value);

Example

let browsers = new Set([

    "Chrome",

    "Firefox"

]);

console.log(browsers.has("Chrome"));

console.log(browsers.has("Safari"));

Output

true
false

Removing All Values (clear())

The clear() method removes every value from the Set.

Syntax

setName.clear();

Example

let cities = new Set([

    "Delhi",

    "Mumbai"

]);

cities.clear();

console.log(cities);

Output

Set(0) {}

Getting the Size of a Set (size)

The size property returns the number of values stored in the Set.

Syntax

setName.size

Example

let languages = new Set([

    "JavaScript",

    "Python",

    "Java"

]);

console.log(languages.size);

Output

3

Iterating Through a Set

You can use the for...of loop to access each value.

let fruits = new Set([

    "Apple",

    "Mango",

    "Orange"

]);

for (let fruit of fruits) {

    console.log(fruit);

}

Output

Apple
Mango
Orange

Real-World Example

Suppose an application stores unique browser names.

let browsers = new Set();

browsers.add("Chrome");

browsers.add("Firefox");

browsers.add("Chrome");

console.log(browsers.size);

Output

2

The duplicate value "Chrome" is ignored.


Another example:

Remove a department.

let departments = new Set([

    "QA",

    "HR",

    "Development"

]);

departments.delete("HR");

console.log(departments);

Output

Set(2) { 'QA', 'Development' }

Automation Testing Example

Automation engineers frequently use Set operations to manage unique browsers, test users, API endpoints, and execution data.

Playwright Example

Store supported browsers.

const browsers = new Set();

browsers.add("chromium");

browsers.add("firefox");

console.log(browsers);

Output

Set(2) { 'chromium', 'firefox' }

Selenium Example

Check browser availability.

const browserSet = new Set([

    "chrome",

    "firefox"

]);

console.log(browserSet.has("chrome"));

Output

true

Cypress Example

Remove a test environment.

const environments = new Set([

    "QA",

    "UAT",

    "Production"

]);

environments.delete("UAT");

console.log(environments);

Output

Set(2) { 'QA', 'Production' }

API Testing Example

Store unique endpoints.

const endpoints = new Set();

endpoints.add("/users");

endpoints.add("/orders");

console.log(endpoints.size);

Output

2

Data-Driven Testing Example

Store unique usernames.

const users = new Set();

users.add("admin");

users.add("tester");

users.add("admin");

console.log(users);

Output

Set(2) { 'admin', 'tester' }

Common Mistakes

Expecting add() to Store Duplicates

let set = new Set();

set.add(10);

set.add(10);

console.log(set);

Output

Set(1) { 10 }

Sets automatically ignore duplicate values.


Using length Instead of size

Incorrect:

console.log(set.length);

Output:

undefined

Correct:

console.log(set.size);

Expecting delete() to Remove by Index

Incorrect:

set.delete(0);

The delete() method removes a value, not an index.

Correct:

set.delete("Apple");

Best Practices

Use Sets for Unique Collections

Choose a Set when duplicate values should not be stored.


Use has() Before Processing

Check whether a value exists before performing operations that depend on it.


Use Meaningful Variable Names

Instead of:

let s = new Set();

Use:

let browserSet = new Set();

This improves readability.


Use clear() Carefully

clear() removes every value from the Set. Use it only when you intend to empty the entire collection.


Conclusion

Set operations provide an efficient way to manage collections of unique values in JavaScript. Methods such as add(), delete(), has(), clear(), and the size property make it easy to create, update, search, and manage Sets.

For automation engineers, Set operations are particularly valuable for handling unique browser names, test users, API endpoints, execution IDs, and other duplicate-free datasets. Mastering these operations helps build cleaner, more efficient, and reliable JavaScript automation scripts.


Frequently Asked Questions (FAQs)

What is the purpose of the add() method?

It adds a new value to the Set. Duplicate values are ignored.


How do you remove a value from a Set?

setName.delete(value);

How do you check if a value exists?

setName.has(value);

How do you remove all values from a Set?

setName.clear();

How do you find the number of values in a Set?

Use the size property.

console.log(setName.size);

Why are Set operations important in automation testing?

Automation engineers use Set operations to manage unique browser names, user IDs, API endpoints, test environments, and other collections where duplicate values must be avoided.


Key Takeaways

  • Sets store only unique values.

  • add() inserts a new value into a Set.

  • Duplicate values are automatically ignored.

  • delete() removes a specific value.

  • has() checks whether a value exists.

  • clear() removes all values from the Set.

  • size returns the number of values in the Set.

  • for...of can be used to iterate through Set values.

  • Set operations are widely used in modern JavaScript and automation testing.

  • Mastering Set operations helps manage unique collections efficiently.