Set Methods

Introduction

A Set in JavaScript is a collection of unique values. JavaScript provides several built-in methods that allow you to add, remove, check, retrieve, and iterate over Set values efficiently.

These methods make Sets easy to use when working with collections where duplicate values are not allowed.

Set methods were introduced in ES6 (ECMAScript 2015) and are widely used in modern JavaScript development, Node.js applications, API testing, and automation frameworks such as Selenium, Playwright, and Cypress.

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


Common Set Methods

The following are the most commonly used Set methods and properties:

Method / PropertyDescription
add()Adds a new 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.
values()Returns an iterator containing all Set values.
keys()Returns an iterator of all Set values (same as values()).
entries()Returns key-value pairs for each Set value.
forEach()Executes a function for each value in the Set.

add() Method

The add() method inserts a value into the Set.

Syntax

setName.add(value);

Example

let fruits = new Set();

fruits.add("Apple");

fruits.add("Mango");

console.log(fruits);

Output

Set(2) { 'Apple', 'Mango' }

delete() Method

The delete() method removes a 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' }

has() Method

The has() method checks whether a value exists.

Syntax

setName.has(value);

Example

let browsers = new Set([

    "Chrome",

    "Firefox"

]);

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

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

Output

true
false

clear() Method

The clear() method removes all values from the Set.

Syntax

setName.clear();

Example

let cities = new Set([

    "Delhi",

    "Mumbai"

]);

cities.clear();

console.log(cities);

Output

Set(0) {}

size Property

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

Syntax

setName.size

Example

let languages = new Set([

    "JavaScript",

    "Python",

    "Java"

]);

console.log(languages.size);

Output

3

values() Method

The values() method returns an iterator containing all Set values.

let fruits = new Set([

    "Apple",

    "Mango"

]);

for (let value of fruits.values()) {

    console.log(value);

}

Output

Apple
Mango

keys() Method

For Sets, keys() behaves the same as values().

let numbers = new Set([

    10,

    20

]);

for (let key of numbers.keys()) {

    console.log(key);

}

Output

10
20

entries() Method

The entries() method returns [value, value] pairs because Sets do not have separate keys.

let colors = new Set([

    "Red",

    "Blue"

]);

for (let entry of colors.entries()) {

    console.log(entry);

}

Output

[ 'Red', 'Red' ]
[ 'Blue', 'Blue' ]

forEach() Method

The forEach() method executes a function once for each Set value.

let fruits = new Set([

    "Apple",

    "Mango",

    "Orange"

]);

fruits.forEach(function(fruit) {

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

console.log(browsers.size);

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

Output

2
true

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 use Set methods to manage unique browsers, users, environments, and API endpoints.

Playwright Example

Store supported browsers.

const browsers = new Set();

browsers.add("chromium");

browsers.add("firefox");

console.log(browsers.size);

Output

2

Selenium Example

Check browser support.

const browserSet = new Set([

    "chrome",

    "firefox"

]);

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

Output

true

Cypress Example

Iterate through environments.

const environments = new Set([

    "QA",

    "UAT",

    "Production"

]);

environments.forEach(function(env) {

    console.log(env);

});

Output

QA
UAT
Production

API Testing Example

Store unique endpoints.

const endpoints = new Set();

endpoints.add("/users");

endpoints.add("/orders");

console.log(endpoints);

Output

Set(2) { '/users', '/orders' }

Data-Driven Testing Example

Store unique usernames.

const usernames = new Set();

usernames.add("admin");

usernames.add("tester");

usernames.add("admin");

console.log(usernames.size);

Output

2

Common Mistakes

Using length Instead of size

Incorrect:

console.log(mySet.length);

Output:

undefined

Correct:

console.log(mySet.size);

Expecting keys() to Return Indexes

Sets do not have indexes.

let set = new Set(["A", "B"]);

keys() returns:

A
B

Expecting entries() to Return Different Keys and Values

Output:

[ 'A', 'A' ]
[ 'B', 'B' ]

Since Sets store only values, both the key and value are the same.


Best Practices

Use has() Before Processing

Check if a value exists before using it.


Use forEach() or for...of for Iteration

Both provide a clean way to process all Set values.


Use Meaningful Variable Names

Instead of:

let s = new Set();

Use:

let browserSet = new Set();

Choose Sets Only When Uniqueness Is Required

If duplicate values or indexed access are needed, use an array instead.


Conclusion

JavaScript Set methods provide a simple and efficient way to manage collections of unique values. Methods such as add(), delete(), has(), clear(), values(), keys(), entries(), and forEach() make it easy to manipulate and iterate through Sets.

For automation engineers, these methods are particularly useful for handling unique browser names, API endpoints, user IDs, execution results, and environment configurations. Mastering Set methods helps write cleaner, more efficient, and maintainable JavaScript automation code.


Frequently Asked Questions (FAQs)

What method adds a value to a Set?

setName.add(value);

Which method removes a value?

setName.delete(value);

How do you check if a value exists?

setName.has(value);

How do you remove all values?

setName.clear();

Which property returns the number of values?

Use the size property.

console.log(setName.size);

Why are Set methods important in automation testing?

Automation engineers use Set methods to manage unique browser names, test users, API endpoints, environments, execution IDs, and other duplicate-free collections efficiently.


Key Takeaways

  • Sets store only unique values.

  • add() inserts new values into a Set.

  • delete() removes specific values.

  • has() checks whether a value exists.

  • clear() removes all values from a Set.

  • size returns the total number of values.

  • values() and keys() return iterators for Set values.

  • entries() returns [value, value] pairs.

  • forEach() executes a function for each Set value.

  • Mastering Set methods is essential for writing efficient JavaScript and automation testing code.