EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials TypeScript Tutorial TypeScript Set
Secondary Sidebar
TypeScript Tutorial
  • TypeScript Basic and Advanced
    • What is TypeScript?
    • Typescript Examples
    • TypeScript Versions
    • TypeScript Operators
    • JavaScript dump object
    • JavaScript get Method
    • Webpack ReactJS
    • Code Generator JavaScript
    • JavaScript Projects
    • Call Stack JavaScript
    • JavaScript Projects GitHub
    • JavaScript Filter Function
    • JavaScript nan
    • JavaScripttimestamp
    • TypeScript loop
    • CoffeeScript
    • TypeScript Webpack
    • setTimeout TypeScript
    • DHTMLX
    • CoffeeScript for loop
    • TypeScript number
    • JavaScript export module
    • TypeScript string contains
    • TypeScript Inheritance
    • TypeScript get
    • TypeScript undefined
    • TypeScript Global Variable
    • TypeScript Dictionary
    • TypeScript Generic
    • TypeScript Cast Object
    • TypeScript Optional Parameters
    • TypeScript? switch
    • TypeScript promise
    • TypeScript tuple
    • TypeScript Hashmap
    • TypeScript let
    • TypeScript Getter
    • TypeScript Pattern Matching
    • TypeScript number to string
    • TypeScript substring
    • TypeScript?lambda
    • TypeScript UUID
    • TypeScript JSDoc
    • TypeScript Decorators
    • Typescript for loop
    • TypeScript HTTP Request
    • TypeScript Abstract Class
    • TypeScript Question Mark
    • TypeScript Nullable
    • TypeScript reduce
    • TypeScript Mixins
    • TypeScript keyof
    • TypeScript string to number
    • TypeScript JSON parse
    • TypeScript const
    • TypeScript declare module
    • TypeScript String
    • TypeScript filter
    • TypeScript Multiple Constructors
    • TypeScript? Set
    • TypeScript string interpolation
    • TypeScript instanceof
    • TypeScript JSON
    • TypeScript Arrow Function
    • TypeScript generator
    • TypeScript namespace
    • TypeScript default parameter
    • TypeScript cast
    • TypeScript babel
    • Typescript Key-Value Pair
    • TypeScript if
    • TypeScript keyof Enum
    • TypeScript wait
    • TypeScript Optional Chaining
    • TypeScript JSX
    • TypeScript Version Check
    • TypeScript Unit Testing
    • TypeScript Handbook
    • TypeScript module
    • TypeScript Extend Interface
    • TypeScript npm
    • TypeScript pick
    • TypeScript Interface Default Value
    • JavaScript import module
    • Obfuscate Javascript
    • TypeScript basics
    • setInterval TypeScript
  • Type of Union
    • TypeScript Object Type
    • TypeScript type check
    • TypeScript promise type
    • TypeScript JSON type
    • TypeScript Union Types
    • TypeScript typeof
    • TypeScript Types
  • TypeScript Array
    • TypeScript Array of Objects
    • Methods TypeScript Array
    • TypeScript remove item from array
    • TypeScript add to array
    • TypeScript Array Contains
  • Function Of Array
    • TypeScript Function Interface
    • TypeScript Functions
    • TypeScript Export Function
    • TypeScript function return type

TypeScript Set

By Princy VictorPrincy Victor

TypeScript Set

Definition of TypeScript Set Method

In TypeScript, a novel data structure has been added to ES6 JavaScript version known as set. This helps in storing data of distinct values that occurs only once into a list that is available in different programming languages like C#, Java, etc. Even though this data structure is almost similar to maps, it cannot store pairs of key-values, but only keys. As the objects of set are a group of values, it can be iterated through those items in a particular order. In this article, we will be discussing the different aspects of set in typescript.

Syntax:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Below is the syntax of typescript set.

let setobj = new Set();

Here, set type has to be used along with the keyword new.

Methods

Following are the different methods of set in Typescript.

  • Method:
    add(val)

Description: This method helps in adding values to the set.

  • Method:
    has(val)

Description: This method checks whether the value passed in the method is present in the set or not. If it is present, true will be returned. Else, false will be returned.

  • Method:
    delete()

Description: This method helps in removing values from the set.

  • Method:
    size()

Description: This method helps in returning size of the set.

  • Method:
    clear()

Description: This method helps in removing all values from the set.

Examples

Let us discuss Examples of TypeScript Set.

Example #1: Typescript program that creates a set and add values to it.

Code:

//create a set with marks as object
let marks = new Set();
//Add marks to the set
marks.add(21);
marks.add(23);
marks.add(13);
console.log("The marks in the Set are:");
console.log(marks);

Sample Output:

TypeScript set 1

In this program, a set is created with marks as object. Then, elements such as 21, 23, and 13 are added to it. On executing the code, all elements in the set are displayed.

Example #2: Typescript program that creates a set and add values to it using add() method chaining

Code:

//create a set with marks as object
let marks = new Set();
//Add marks to the set
marks.add(21).add(23).add(13);
console.log("The marks in the Set are:");
console.log(marks);

Sample Output:

TypeScript set 2

In this program also, a set is created with marks as object. Then, elements such as 21, 23, and 13 are added to it. The difference of this program with the above program is that here the elements are added to the set by chaining the add() method. On executing the code, all elements in the set are displayed.

Example #3: Typescript program that creates a set and checks whether a particular value is present in it.

Code:

//create a set with marks as object
let marks = new Set();
//Add marks to the set
marks.add(21).add(23).add(13);
console.log("The marks in the Set are:");
console.log(marks);
//Check whether the value 21 and 34 is present in the set or not
console.log(marks.has(21));
console.log(marks.has(34));

Sample Output:

TypeScript set 3

In this program also, a set is created with marks as object. After adding the elements such as 21, 23, and 13 are added to it, elements 21, and 34 are checked whether they are present in the available set. As element 21 is present and 34 is absent, true and false will be returned respectively.

Example #4: Typescript program that creates a set and finds the size of the set.

Code:

//create a set with marks as object
let marks = new Set();
//Add marks to the set
marks.add(21).add(23).add(13);
console.log("The marks in the Set are:");
console.log(marks);
//returns the size of the set
console.log("The size of the set is:");
console.log(marks.size);

Sample Output:

example 4

In this program also, a set is created with marks as object. After adding the elements such as 21, 23 and 13 are added to it, size of the set is identified using the size() method as shown in the program. As there are three elements in the set, 3 will be returned as the size of the set.

Example #5: Typescript program that creates a set and removes a particular element from it.

Code:

//create a set with marks as object
let marks = new Set();
//Add marks to the set
marks.add(21).add(23).add(13) ;
console.log("The marks in the Set are:") ;
console.log(marks);
//returns the size of the set
console.log("The size of the set is:");
console.log(marks.size) ;
//deletes an element from the set
console.log(marks.delete(23) ) ;
console.log("The updated size of the set is:") ;
console.log(marks.size) ;
console.log("The updates elements in the Set are:");
console.log(marks);

Sample Output:

example 5

In this program also, a set of marks created with elements such as 21, 23, and 13. Then, the size of the set is identified using the size() method. Once this is done, element 21 is removed and again the size of the set is identified. On executing the code, it can be seen that size of the set is reduced to 2 and the updated set does not contain 21 as element.

Example #6: Typescript program that creates a set and removes all the elements from it.

Code:

//create a set with marks as object
let marks = new Set();
//Add marks to the set
marks.add(21).add(23).add(13);
console.log("The marks in the Set are:");
console.log(marks) ;
//returns the size of the set
console.log("The size of the set is:") ;
console.log(marks.size) ;
//clears all elements from the set
marks.clear() ;
console.log("The updated size of the set is:") ;
console.log(marks.size) ;
console.log("The updates elements in the Set are:") ;
console.log(marks) ;

Sample Output:

example 6

In this program, a set of marks created with elements such as 21, 23, and 13. Then, the size of the set is identified using the size() method. Once this is done, all elements are removed, and again the size of the set is identified. On executing the code, it can be seen that size of the set is reduced to 0 and the updated set does not contain any elements.

Conclusion

A data structure known as set has been added to ES6 JavaScript that helps in storing data of distinct values that occurs only once into a list that is available in different programming languages like C#, Java, etc. In this article, different aspects such as syntax, methods, and examples of set in typescript are explained in detail.

Recommended Articles

We hope that this EDUCBA information on “TypeScript Set” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. TypeScript Cast Object
  2. TypeScript typeof
  3. TypeScript Optional Parameters
  4. TypeScript Array
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more