VOOZH about

URL: https://www.geeksforgeeks.org/javascript/sets-in-javascript/

⇱ Set in JavaScript - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Set in JavaScript

Last Updated : 25 Sep, 2025

A Set in JavaScript is used to store a unique collection of items, meaning no duplicates are allowed.

  • Sets internally use a hash table which makes search, insert and delete operations faster than arrays. Please note that a hash table data structure allows these operations to be performed on average in constant time.
  • Set maintains the insertion of items. When we access items, we get them in the same order as inserted.
  • Only unique values are allowed. If you try to add a duplicate value, it will be ignored.
  • A set can be created either empty or by providing an iterable like array or string.

Output
Set(3) { 10, 30, 40 }
Set(2) { 'gfg', 'geeks' }
Set(3) { 'f', 'o', 'd' }
Set(0) {}

Key Characteristics of Sets

  • Unique Values: Sets only contain unique elements, automatically removing duplicates.
  • Ordered: Sets maintain the order of elements as they are inserted.
  • Iterable: You can iterate through Sets using loops like for...of or forEach.
  • No Indexing: Sets don’t support indexing; you must iterate to access elements.

Methods of Set in JavaScript

JavaScript’s Set object comes with several built-in methods to add, remove, check, and iterate over values. Below are the most commonly used methods with syntax and examples.

Set.add() :

Adds the new element with a specified value at the end of the Set object.

Syntax :

 mySet.add(value);

Example : In this example, we have added two numbers 23 and 12 into the set.


Output
Set(2) { 23, 12 }

Set.delete() :

deletes an element with the specified value from the Set object. 

Syntax :

mySet.delete(value);

Example : Removing Elements from a Set with the delete() Method.


Output
Set(2) { 75, 12 }
true
Set(1) { 12 }

Set.clear() :

The Set clear() method in JavaScript is used for the removal of all the elements from a set and making it empty.

Syntax:

mySet.clear();

Example : Emptying set using Set clear() method.


Output
Set(1) { 23 }
1
0

Set.entries() :

Returns an iterator object which contains an array having the entries of the set, in the insertion order. 

Syntax :

mySet.entries()

Example :


Output
[ 'California', 'California' ]
[ 'Seattle', 'Seattle' ]
[ 'Chicago', 'Chicago' ]

Set.has() :

Returns true if the specified value is present in the Set object. 

Syntax :

mySet.has(value);

Example : In this example, we have used sethas() method.


Output
true

Set.values() :

Returns all the values from the Set in the same insertion order.

Syntax:

mySet.values()

Example :


Output
California
Seattle
Chicago

Set.keys() :

Also returns all the values from the Set in the insertion order. 

Syntax :

keys()

Example : In this example, we will see the use of keys in the set.


Output
[Set Iterator] { 'America', 'England', 'Chicago' }

Set.forEach() :

executes the given function once for every element in the Set, in the insertion order.

Syntax :

forEach(function(value, key, set) { 
/* ... */
}, thisArg)

Example : In this example, we will see the use of the forEach() method.

Comment