Set in ES6

Set in ES6

A Set is a kind of object used to create a group of unique values. The values stored in a set can be string, literal, array, and integer. The set only deal with a single value or single object (Object reference).

The set is similar to an array, but it doesn’t involve any duplicate element. The sets are always defined in an ordered form similar to maps. It means the set elements are iterated in an insertion manner and used to return the set object.

Syntax

var q = new Set (“value1”, “value2”, “value3”);

Example  

Here, we have an example to illustrate the concept of set.

let month = new Set ([“January”, “February”, “March”, “April”, “May”]);

console.log (month);

Output

After the execution of the code, we got the following output:

Set in ES6

Methods of Sets

The set object has various methods which are given in tabular form-

             Sr. No.            Method          Description
                 1Set.prototype.clear()This method is used to delete all the elements of the set object.
                 2Set.prototype.add(value)It is used to affix a new element in the existing value of the set object.
                 3Set.prototype.delete(value)This method is used to delete the element attached to the value.
                 4Set.prototype.has(value)It is used to return true when the passed value lies in the set object.
                 5Set.prototype.entries()This method is used to return the iterator object. This object has an array of set object elements in insertion order.
                 6Set.prototype.values()It is used to return the iterator object, includes an array of set object elements in insertion order.
                 7Set.prototype.forEach(callbackFn[, thisArg])This method is used to run the callback function once only.

Here, we have a detailed description of each method.

Set.prototype.clear()

This particular method is used to delete all the objects of the sets.

Example

We have an example to understand this method-

let days = new Set ([‘Sunday’, ‘Monday’, ‘Tuesday’, ‘Wednesday’]);
days.add(‘Friday’);
days.add(‘Saturday’);
days.add(‘Friday’);
days.add(‘Thursday’);
days.clear();
console.log(days.size);

Output

After the execution of the above code, we got the following output:

Set in ES6

Set.prototype.add(value)

The Set.prototype.add(value) method is used to affix the new elements with set object values.

Example

Here, we have an example to understand this method-

let days = new Set ([‘Sunday’, ‘Monday’, ‘Tuesday’, ‘Wednesday’]);
days.add(‘Friday’);
days.add(‘Saturday’);
days.add(‘Thursday’);
days.add(‘Friday’);
days.clear();
console.log(days.size);

Output

After the execution of the above code, we got the following output:

Set in ES6

Set.prototype.delete(value)

This method is used to delete the element attached to the set object.

Example

Here, we have an example to understand this method-

let days = new Set ([‘Sunday’, ‘Monday’, ‘Tuesday’, ‘Wednesday’]);
days.add(‘Friday’);
days.add(‘Saturday’);
days.add(‘Thursday’);
days.add(‘Friday’);
days.delete(‘Friday’);
console.log(days.size);
console.log(days);

Output

After the execution of the above code, we got the following output:

Set in ES6

Set.prototype.has(value)

This method is used to return true or false that defines that the element exists in the Set object or not.   

Example

Here, we have an example to understand this method-

let days = new Set ([‘Sunday’, ‘Monday’, ‘Tuesday’, ‘Wednesday’]);
days.add(‘Friday’);
days.add(‘Saturday’);
days.add(‘Thursday’);
days.add(‘Friday’);
console.log(days.has(‘ Friday’);
console.log(days.has(‘Saturday’);
console.log(days.has(‘Sunday’);

Output

After the execution of the above code, we got the following output:

Set in ES6

Set.prototype.entries()

This method is used to return the object set of new iterators. It includes the array value for each element. The insertion order follows this insertion method.

Example

Here, we have an example to understand this method-

let days = new Set ([‘Sunday’, ‘Monday’, ‘Tuesday’, ‘Wednesday’]);
days.add(‘Friday’);
days.add(‘Saturday’);
days.add(‘Thursday’);
days.add(‘Friday’);
var iterator = days.entries();
for(i=0;i<days;i++)
{
          console.log(iterator.next().value);
}

Output

After the execution of the above code, we got the following output:

Set in ES6

Set.prototype.values()

This method is used to return the iterator object which includes an array of set object elements in insertion order.

Example

Here, we have an example to understand this method-

let days = new Set ([‘Sunday’, ‘Monday’, ‘Tuesday’, ‘Wednesday’]);
days.add(‘Thursday’);
var value = days.values();
console.log(value.next().value);
console.log(value.next().value);
console.log(value.next().value);
console.log(value.next().value);
console.log(value.next().value);

Output

After the execution of the above code, we got the following output:

Set in ES6

Set.prototype.forEach(callbackFn[, thisArg])

This method is used to execute the particular callback function once only.

Example

Here, we have an example to understand this method-

let days = new Set ([‘Sunday’, ‘Monday’, ‘Tuesday’, ‘Wednesday’]);
days.add(‘Friday’);
days.add(‘Saturday’);
days.add(‘Thursday’);
days.add(‘Friday’);
function details(values)
{
          console.log(values);
}
days.forEach(details);

Output

After the execution of the above code, we got the following output:

Set in ES6

Properties of Set

            Sr. No.           Properties           Description
                 1Set.sizeThis property is used to return the values in the set object.

Set.size

It is a property of the set object. This specified property is used to return the value, which presents the elements in the set object.

Example

let month = new Set([‘January’, ‘February’, ‘March’, ‘April’, ‘February’]);
console.log(month.size);
console.log(month);

Output

After the execution of the above code, we got the following output:

Set in ES6

Weak Set

The weak set helps us to store the group of objects. Weak set contains add(value), has(value), delete(value) set object methods. The weak sets are similar to set objects, but they cannot store the replicate values. We cannot iterate the weak set similar to weak maps. The objects which are stored in a weak set only can be in garbage collection.

Example

Here, we have the following example to elaborate the weak set.

‘use strict’
let weakest = new weak Set();
let object = {message: “Welcome to ES6!”};
weakset.add(object);
console.log(weakest.has(object));
weakest.delete(object);
console.log(weakest.has(object));

Output

Set in ES6

Iterator

The iterator can be defined as an object, used to determine the sequence and return a value at the time of termination. The iterator allows to access the set of one object at the same time.

The Set object and Map object both have the methods that return an iterator. The Iterator can be considered as objects with the next method (). When we call the next method, then the iterator returns an object that includes the value and done properties. The done returns a Boolean value; true if it accesses all elements of the set object otherwise false.  

Example

We have an example to illustrate the iterator implementation with the Set object.

let month = new Set([‘January’, ‘February’, ‘March’, ‘April’, ‘May’]);
var iterator = months.keys();
var iterator = months.entries();
var iterator = months.values();
console.log(iterator.next());
console.log(iterator1.next());
console.log(iterator2.next());

Output

After the execution of the above example, we got the following output:

Set in ES6

Related Topics

Boolean in ES6

Boolean in ES6 In ES6, the Boolean object is used to show two values either it may be true or false. In JavaScript, we can use the Boolean object as a...

4 minutes read.

Array Methods in ES6

Array Methods in ES 6 ES 6 introduced the various new methods such as Array,from(), Array,of(). Here, we have the various array methods used in JavaScript, they are given below in the table...

10 minutes read.

Array Destructuring in ES6

Array Destructuring in ES6 The term ‘Destructuring’ is defined as dividing the complicated data structure into easier modules. The Destructuring syntax helps to extract the small segments from an array and the objects....

3 minutes read.

Void Keyword in ES6

Void Keyword in ES6 In ES6, the void keyword is referred to as a function return type, but it does not return any value. It classifies the expression and returns the...

2 minutes read.

Animation in ES6

Animation in ES6 In JavaScript, the animation feature is used to manage the aspect that the CSS (Cascading Style Sheet) can’t. JavaScript provides various new elements that help us to construct...

8 minutes read.

Template Literals in ES6

Template Literals in ES6            ES6 introduced a new feature called template literals. It facilitates us to define a multiline string and executes the string interpolation. The template literals are referred to...

3 minutes read.

Module in ES6

Module in ES6 In ES6, the modules can be defined as a small chunk of program code stored in a file. The modules are used to modularize the source code with...

6 minutes read.

Page Redirect in ES6

Page Redirect in ES6 The term redirect can be defined as a process to send the user and search engine on a specified URL from the original page. “The page redirection is...

3 minutes read.

Map Object in ES 6

Map Object in ES 6 ES 6 introduced a new feature that is included in JavaScript. In the previous versions of ECMAScript, if we want to perform mapping on values and...

5 minutes read.

ES6 | ECMAScript 6 Syntax

The term 'syntax' defines the grammar and spelling of any programming language. In Computer science, “Term syntax is a collection of some rules and regulations that determines the group of symbols used in...

8 minutes read.

ES6 Tutorial

Introduction of ES6 The ECMAScript 6 is a scripting language specification institutionalized by ECMA International. ECMA, named in 1994, is an international organization, related to information and communication system. ECMA stands for “European Computer Manufacturers...

8 minutes read.

Arrow Function in ES6

Arrow Function in ES6  An Arrow function is a new concept recommended in ECMAScript 2015. It is also called “Fat Arrow Function.” It helps us to write the function more accurately. This function...

5 minutes read.

Number in ES6

Number in ES6 In ES6, we have various methods and properties to implement numeric functions such as floating-point, integers, date, etc. The numbers in ES6 enable us to work with number...

7 minutes read.

Loop Control Statement in ES6

Loop Control Statement in ECMAScript 6 The loop control statements can be defined as a set of conditions that control the execution of the loop. It executes the loop until the condition becomes false....

6 minutes read.

Loops in ES6 | ECMAScript 6

Loops in ECMAScript 6 A Loop statement can be defined as a group of instructions that are always repeated. In the computer programming language, the loops help us to execute a collection of instructions...

8 minutes read.

Variables in ES6 | ECMAScript 6

Variables in ECMAScript 6 A Variable is a place in memory used to store a value. There are some rules for declaration of the variable- Variable Initialization Initialization is a process of locating and storing the initial...

4 minutes read.

Validation in ES6

Validation in ES6 The term validation can be defined as a mechanism to check the information given by the user is correct or not according to the requirements. If the data...

9 minutes read.

Promises in ES6

Promises in ES6 A promise is used to represent some aspects that are finally fulfilled. It can also be resolved or discarded according to the operational output. In ES6, a promise...

8 minutes read.

Object Destructuring in ES6

Object Destructuring in ES6 The term destructuring is referred to as the breakdown of the complex entities into simpler parts. It works like array destructuring. In the array destructuring, the values...

3 minutes read.

Generator in ES6 | ECMAScript 6

ECMAScript 6 recommends a new way of working with iterators and functions. The way is named as generator or generator function. The ES 6 generator is a distinct type of function that...

5 minutes read.