EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Software Development Software Development Tutorials TypeScript Tutorial TypeScript enum
 

TypeScript enum

Updated March 28, 2023

TypeScript enum

 

 

Introduction to TypeScript enum

The new data type supported in TypeScript is called enumerations or enum using which lets us declare names which means a collection of related values which can be either string or numeric, and there are three types of enums in TypeScript, namely Numeric enum, string enum and Heterogeneous enum where numeric enum are the enums based on numbers that are they store numbers in the form of the string, string enum is the enums based on strings that is they store string values, heterogeneous enum are the enums based on both strings and numbers that is they store both string and numeric values.

Watch our Demo Courses and Videos

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

The syntax to declare enum in TypeScript is as follows:

enum enum_name{
enum_value1,
enum_value2,
..
Enum_valuen
}

where enum_name represents the name of the enum,

enum_value1, enum_value2, and enum_valuen represent the collection of related values, either string or numeric.

Working of enum in TypeScript

  • The new data type supported in TypeScript is called enumerations or enum using which lets us declare names which means a collection of related values that can be either string or numeric.
  • The keyword enum is used to define enum in TypeScript.
  • There are three types of enum in TypeScript, namely Numeric enum, string enum, and Heterogeneous enum.
  • Numeric enum is the enums based on numbers; they store numbers in the form of strings.
  • The enum values for numeric enum begin from zero and increase by one for each number.
  • There is also an option to initialize the first enum value ourselves in a numeric enum, and the rest of the values would be increased by one.
  • The enum values do not have to be in sequence in the numeric enum.
  • String enum is the enums based on strings that is they store string values.
  • The readability of the program increases by using a string enum.
  • Heterogeneous enum is the enums based on both strings and numbers; they store both string and numeric values.

Examples of TypeScript enum

Different examples are mentioned below:

Example #1

TypeScript program to demonstrate the usage of numeric enum that initializes values and are displayed as the output on the screen:

Code:

//enum keyword is used to define a related collection of numeric values
enum Country
{
India,
USA,
Germany,
London
}
//displaying the values that are initialized by enum
console.log("The values initialized by enum is as follows:");
console.log(Country.India);
console.log(Country.USA);
console.log(Country.Germany);
console.log(Country.London);

Output:

TypeScript enum output 1

In the above program, by using the keyword enum, we define a numeric enum whose values are initialized starting from zero and incremented by one for each member. Then the initialized values for the numeric enum are displayed as the output on the screen.

Example #2

TypeScript program to demonstrate the usage of numeric enum that initializes values where the first value is initialized by ourselves and are displayed as the output on the screen:

Code:

//enum keyword is used to define a related collection of numeric values in which the first value is initialized by ourselves as 5
enum Country
{
India = 5,
USA,
Germany,
London
}
//displaying the values that are initialized by enum
console.log("The values initialized by enum is as follows:");
console.log(Country.India);
console.log(Country.USA);
console.log(Country.Germany);
console.log(Country.London);

Output:

TypeScript enum output 2

In the above program, by making use of the keyword enum, we are defining a numeric enum in which the first value is initialized as five by ourselves and then incremented by one for each member. Then the initialized values for the numeric enum are displayed as the output on the screen.

Example #3

TypeScript program to demonstrate the usage of string enum that initializes string values and are displayed as the output on the screen:

Code:

//enum keyword is used to define a related collection of string values
enum Capital
{
India = 'New Delhi',
USA = 'Washington',
Germany = 'Berlin',
England = 'London'
}
//displaying the values that are initialized by enum
console.log("The values initialized by string enum is as follows:");
console.log(Capital.India);
console.log(Capital.USA);
console.log(Capital.Germany);
console.log(Capital.England);

Output:

output 3

In the above program, by using the keyword enum, we define a string enum whose values are initialized to certain string values. Then the initialized values for the string enum is displayed as the output on the screen.

Example #4

TypeScript program to demonstrate the usage of heterogeneous enum that initializes both string values and numeric values and are displayed as the output on the screen:

Code:

//enum keyword is used to define a related collection of both string values and numeric values
enum Capital
{
India = 1,
USA = 'Washington',
Germany = 3,
England = 'London'
}
//displaying the values that are initialized by enum
console.log("The values initialized by heterogeneous enum is as follows:");
console.log(Capital.India);
console.log(Capital.USA);
console.log(Capital.Germany);
console.log(Capital.England);

Output:

output 4

In the above program, by using the keyword enum, we define a heterogeneous enum whose values are initialized to certain string values and certain numeric values. Then the initialized values for the heterogeneous enum is displayed as the output on the screen.

Example #5

TypeScript program to demonstrate the usage of heterogeneous enum that initializes both string values and numeric values and are displayed as the output on the screen:

Code:

//enum keyword is used to define a related collection of both string values and numeric values
enum Capital
{
India = 8,
USA = 12,
Germany = 'Berlin',
England = 'London'
}
//displaying the values that are initialized by enum
console.log("The values initialized by heterogeneous enum is as follows:");
console.log(Capital.India);
console.log(Capital.USA);
console.log(Capital.Germany);
console.log(Capital.England);

Output:

output 5

In the above program, by using the keyword enum, we define a heterogeneous enum whose values are initialized to certain string values and certain numeric values. Then the initialized values for the heterogeneous enum is displayed as the output on the screen.

Recommended Articles

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

  1. TypeScript Types
  2. How to Install Typescript
  3. TypeScript Array
  4. TypeScript Functions

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
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
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

*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