Open In App

JavaScript Set Reference

JavaScript Set is a collection of items that are unique i.e. 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:



Set.function()

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




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 gets called when an object is created using the new keyword.

Constructor Description Example
Set() Create a set of unique values of any type, whether primitive values or object preferences.
Try

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

Instance Properties Description Example
size Returns the size of set
Try
constructor Used to create a new instance of set
Try

JavaScript Set Methods: Methods are actions that can be performed on objects.

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

Article Tags :