codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🌐

JavaScript

35 / 65 topics
35JavaScript Sets36JavaScript Maps37JavaScript Dates38JavaScript Symbols
Tutorials/JavaScript/JavaScript Sets
🌐JavaScript

JavaScript Sets

Updated 2026-05-12
15 min read

JavaScript Sets

In this tutorial, we will explore the Set object in JavaScript, which is a collection of unique values. Unlike arrays, sets automatically ensure that all elements are distinct, making them ideal for scenarios where duplicates need to be avoided. We'll cover how to create sets, add and remove elements, check for the presence of elements, and perform other useful operations.

Introduction

Sets are particularly useful in situations where you need a collection of unique items without worrying about duplicates. For example, if you're collecting user IDs from a form submission, using a set ensures that each ID is stored only once, preventing any duplication.

Understanding sets is essential for efficient data management and can help avoid common pitfalls related to duplicate values in collections.

Creating a Set

You can create a new set by using the Set constructor. You can also initialize it with an array of values.

Basic Example

createSet.js
1const uniqueNumbers = new Set([1, 2, 3, 4, 5]);
2console.log(uniqueNumbers);
Output
Set { 1, 2, 3, 4, 5 }

Adding Elements

To add elements to a set, use the add() method. This method adds an element to the set if it does not already exist.

addElement.js
1const uniqueNumbers = new Set([1, 2, 3]);
2uniqueNumbers.add(4);
3console.log(uniqueNumbers);
Output
Set { 1, 2, 3, 4 }

Removing Elements

To remove an element from a set, use the delete() method. This method removes the specified element from the set and returns true if the element was successfully removed, or false otherwise.

removeElement.js
1const uniqueNumbers = new Set([1, 2, 3, 4]);
2uniqueNumbers.delete(3);
3console.log(uniqueNumbers);
Output
Set { 1, 2, 4 }

Checking for Elements

To check if a set contains a specific element, use the has() method. This method returns true if the element is in the set, or false otherwise.

checkElement.js
1const uniqueNumbers = new Set([1, 2, 3, 4]);
2console.log(uniqueNumbers.has(2)); // true
3console.log(uniqueNumbers.has(5)); // false
Output
true
false

Iterating Over a Set

You can iterate over the elements of a set using a for...of loop or by converting it to an array and using array methods.

Using for...of Loop

iterateSet.js
1const uniqueNumbers = new Set([1, 2, 3, 4]);
2for (let num of uniqueNumbers) {
3 console.log(num);
4}
Output
1
2
3
4

Converting to Array

You can also convert a set to an array using the Array.from() method or the spread operator.

convertToArray.js
1const uniqueNumbers = new Set([1, 2, 3, 4]);
2const numbersArray = Array.from(uniqueNumbers);
3console.log(numbersArray);
Output
[1, 2, 3, 4]

Practical Example

Let's create a practical example where we use a set to store unique user IDs from form submissions.

Complete Program

uniqueUserIds.js
1const userIds = new Set();
2
3function addUser(userId) {
4 if (userIds.has(userId)) {
5 console.log('User ID already exists.');
6 } else {
7 userIds.add(userId);
8 console.log(`User ID ${userId} added.`);
9 }
10}
11
12addUser(101); // User ID 101 added.
13addUser(102); // User ID 102 added.
14addUser(101); // User ID already exists.
15
16console.log('Unique user IDs:', Array.from(userIds));
Output
User ID 101 added.
User ID 102 added.
User ID already exists.
Unique user IDs: [101, 102]

Summary

ConceptDescription
Creating a SetUse the Set constructor to create a new set. You can initialize it with an array of values.
Adding ElementsUse the add() method to add elements to the set. Duplicate elements are automatically ignored.
Removing ElementsUse the delete() method to remove elements from the set.
Checking for ElementsUse the has() method to check if a set contains a specific element.
Iterating Over a SetUse a for...of loop or convert the set to an array using Array.from() or the spread operator.

What's Next?

Now that you have a good understanding of JavaScript sets, the next topic is JavaScript Maps. Maps provide a way to store key-value pairs and offer more flexibility than sets. We'll explore how maps work, how to create them, add and remove entries, and perform other useful operations.

Stay tuned for our next tutorial!


PreviousJavaScript Getters and SettersNext JavaScript Maps

Recommended Gear

JavaScript Getters and SettersJavaScript Maps