GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • JS Tutorial
  • Web Tutorial
  • A to Z Guide
  • Projects
  • OOP
  • DOM
  • Set
  • Map
  • Math
  • Number
  • Boolean
  • Exercise
  • Introduction3 min read
  • Variables5 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — JavaScript Set Reference - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • JS Tutorial
  • Web Tutorial
  • A to Z Guide
  • Projects
  • OOP
  • DOM
  • Set
  • Map
  • Math
  • Number
  • Boolean
  • Exercise

JavaScript Set Reference

Last Updated : 23 Jul, 2025

JavaScript Set is a collection of items that are unique, meaning no element can be repeated. Elements of the set can be iterated in the insertion order. A Set can store any type of value, whether primitive or objects.

Syntax

new Set()

Example: This example will show the use of Set() constructor.

JavaScript
const mySet = new Set();

mySet.add("California");
mySet.add("India");
mySet.add("California");
mySet.add(10);
mySet.add(10);

const myObject = { a: 1, a: 5 };

mySet.add(myObject);

console.log(mySet);

Output:

Set(4) { 'California', 'India', 10, { a: 5 } }

The complete list of JavaScript Set has listed below:

JavaScript Set Constructor

A constructor is called when an object is created using the new keyword.

ConstructorDescription
Set()Create a set of unique values of any type, whether primitive values or object preferences.

JavaScript Set Properties

 A JavaScript property is a member of an object that associates a key with a value.

Instance Property

An instance property is a property that has a new copy for every new instance of the class.

Instance PropertiesDescription
sizeReturns the size of set
constructorUsed to create a new instance of set

JavaScript Set Methods

Methods are actions that can be performed on objects.

Instance Method

If the method is called on an instance of a Set then it is called an instance method.

Instance MethodsDescription
add()Add an element to the set with the specified value
clear()Removes all the elements from the set
delete()Delete specified element if it exists in the set
entries()Used to iterate over each element of the set
forEach()Applies a function on each element of the set
has()Checks if a particular element exists in the set
values()Returns an iterator that contains all elements of the set
keys()Exactly equivalent to the values method

Comparison with JavaScript Arrays

  • Uniqueness: Sets ensure all values are unique, whereas arrays can have duplicate values.
  • Performance: Sets provide better performance for membership testing and uniqueness checks.

Examples and Use Cases

1. Creating a Set

Creating a Set and adding elements to the set.

JavaScript
const set1 = new Set(['apple', 'banana', 'cherry']);
console.log(set1);

Output
Set(3) { 'apple', 'banana', 'cherry' }

2. Adding and Deleting Elements

Adding new elements and Deleting the elements.

JavaScript
const set2 = new Set();
set2.add(1);
set2.add(5);
set2.add(5); // Duplicate element will not be added
set2.add('Hello');
console.log(set2); 
set2.delete(5);
console.log(set2);

Output
Set(3) { 1, 5, 'Hello' }
Set(2) { 1, 'Hello' }

3. Checking for Element Existence

Checking for already existing elements.

JavaScript
const set3 = new Set(['a', 'b', 'c']);
console.log(set3.has('a'));
console.log(set3.has('z'));

Output
true
false


4. Iterating Over a Set

Iterating over the set and printing one by one.

JavaScript
const set4 = new Set([1, 2, 3, 4]);
for (let item of set4) {
  console.log(item);
}

Output
1
2
3
4

5. Removing Duplicates

Removing the duplicate elements from the array using set.

JavaScript
const numbers = [1, 2, 2, 3, 4, 4, 5];
const uniqueNumbers = [...new Set(numbers)];
console.log(uniqueNumbers);

Output
[ 1, 2, 3, 4, 5 ]


Comment

Explore

    JavaScript Basics
    • Introduction3 min read
    • Variables5 min read
    • Operators4 min read
    • Control Statements4 min read
    Array & String
    • Arrays6 min read
    • Array Methods4 min read
    • String5 min read
    • String Methods8 min read
    Function & Object
    • Functions4 min read
    • Function Expression3 min read
    • Function Overloading4 min read
    • Objects4 min read
    • Constructors4 min read
    OOP
    • OOP5 min read
    • Classes & Objects4 min read
    • Access Modifiers5 min read
    • Constructor4 min read
    Asynchronous JavaScript
    • Asynchronous2 min read
    • Callbacks4 min read
    • Promises4 min read
    • Event Loop3 min read
    • Async Await Fuction3 min read
    Exception Handling
    • Error and Exceptional Handling5 min read
    • Errors Throw & Try to Catch2 min read
    • Custom Errors2 min read
    • TypeError Invalid Array.prototype.sort argument1 min read
    DOM
    • DOM8 min read
    • DOM Elements3 min read
    • Custom Events4 min read
    • Addeventlistener5 min read
    Advanced Topics
    • Closure3 min read
    • Hoisting5 min read
    • Scope4 min read
    • Higher Order Functions7 min read
    • Debugging3 min read
    Courses
    • Placement 360 Course2 min read
    • MERN Full Stack Course2 min read
    • DSA and System Design Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.