EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • All Courses
    • All Specializations
  • Blog
  • Enterprise
  • Free Courses
  • All Courses
  • All Specializations
  • Log in
  • Sign Up
Home Software Development Software Development Tutorials TypeScript Tutorial TypeScript Set
 

TypeScript Set

Princy Victor
Article byPrincy Victor
EDUCBA
Reviewed byRavi Rathore

Updated July 8, 2023

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 occur 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 the 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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax:

Below is the syntax of the typescript set.

let setobj = new Set();

Methods of TypeScript Set

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 the 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: 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 an object. Then, elements such as 21, 23, and 13 are added to it.

Example #2: 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

Example #3: 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

Example #4: 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

Example #5: 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

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

Conclusion

A data structure known as a set has been added to ES6 JavaScript that helps in storing data of distinct values that occur only once into a list that is available in different programming languages like C#, Java, etc.

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
Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - 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
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

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW