Javatpoint Logo
Javatpoint Logo

TypeScript Set

TypeScript set is a new data structure added in ES6 version of JavaScript. It allows us to store distinct data (each value occur only once) into the List similar to other programming languages. Sets are a bit similar to maps, but it stores only keys, not the key-value pairs.

Create Set

We can create a set as below.

Set methods

The TypeScript set methods are listed below.

SN Methods Descriptions
1. set.add(value) It is used to add values in the set.
2. set.has(value) It returns true if the value is present in the set. Otherwise, it returns false.
3. set.delete() It is used to remove the entries from the set.
4. set.size() It is used to returns the size of the set.
5. set.clear() It removes everything from the set.

Example

We can understand the set methods from the following example.

Output:

When we execute the above code snippet, it returns the following output.

TypeScript Set

Chaining of Set Method

TypeScript set method also allows the chaining of add() method. We can understand it from the below example.

Example

Output:

TypeScript Set

Iterating Set Data

We can iterate over set values or entries by using 'for...of' loop. The following example helps to understand it more clearly.

Example

Output:

TypeScript Set




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA