Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • TypeScript Tutorial
  • TS Exercise
  • TS Interview Questions
  • TS Cheat Sheet
  • TS Array
  • TS String
  • TS Object
  • TS Operators
  • TS Projects
  • TS Union Types
  • TS Function
  • TS Class
  • TS Generic
Open In App
Next Article:
TypeScript any Type
Next article icon

TypeScript Enums

Last Updated : 20 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

TypeScript Enums allows you to create a list of constants (unchanging variables) and give them easy-to-remember names. These names make your code easier to read and understand by grouping related values together under one name. Enums can use both numbers and text, so you can choose what works best for your code.

  • Easy-to-remember names: Replace hard-to-understand numbers or strings with meaningful names.
  • Group-related values: Keep similar values organized under a single identifier for better code structure.

Types of Enums in TypeScript

TypeScript provides two main types of enums: Numeric and String enums.

1. Numeric Enums

Numeric enums are the default in TypeScript. Each member of a numeric enum is assigned a numeric value, starting from 0 by default, but you can customize these values as needed.

Default Numeric Enums:

In default numeric enums, the first member is assigned the value 0, and each subsequent member is incremented by 1.

JavaScript
enum Direction {     Up,     Down,     Left,     Right }  let move: Direction = Direction.Up; console.log(move); 
  • First member is 0: Direction.Up is 0.
  • Auto-increment: Direction.Down is 1, Direction.Left is 2, and so on.

Output:

0

Initialized Numeric Enums:

You can assign a specific value to the first member, and subsequent members will auto-increment from that value.

JavaScript
enum Direction {     Up = 1,     Down,     Left,     Right }  let move: Direction = Direction.Up; console.log(move); 
  • Custom start: Direction.Up is set to 1.
  • Auto-increment continues: Direction.Down becomes 2, Direction.Left becomes 3, etc.

Output:

1

Fully Initialized Numeric Enums:

Each member can be assigned a unique numeric value, independent of its position.

JavaScript
enum Direction {     Up = 1,     Down = 3,     Left = 5,     Right = 7 }  let move: Direction = Direction.Up; console.log(move); 
  • Each member has a specific value: Up is 1, Down is 3, Left is 5, and Right is 7.
  • Logging Direction.Up outputs its assigned value, 1.

Output:

1

2. String Enums

String enums allow you to assign string values to each member, providing meaningful names that enhance code clarity.

JavaScript
enum Direction {     Up = "UP",     Down = "DOWN",     Left = "LEFT",     Right = "RIGHT" }  let move: Direction = Direction.Up; console.log(move); 
  • Each member is assigned a descriptive string value.
  • Logging Direction.Up outputs "UP".

Output:

"UP"

3. Heterogeneous Enums

TypeScript also supports heterogeneous enums, where you can mix both numeric and string values in the same enum. However, this is not commonly used because it can make the code less consistent and harder to maintain.

JavaScript
enum Status {     Active = 1,               Inactive = "INACTIVE",      Pending = 2,              Cancelled = "CANCELLED"  }  let currentStatus: Status = Status.Active; console.log(currentStatus); // Output: 1  let cancelledStatus: Status = Status.Cancelled; console.log(cancelledStatus); // Output: "CANCELLED" 
  • Mix of numbers and strings: Members can have either numeric or string values.
  • Less common: Not recommended for most cases as it reduces consistency.

Best Practices of Using TypeScript Enums

  • Use clear and descriptive names: Give enum members meaningful names to make your code easier to read and understand.
  • Prefer const enums for performance: Use const enums in performance-critical code to reduce runtime overhead.
  • Avoid mixing numbers and strings: Stick to either numeric or string enums for consistency and simplicity.
  • Use string enums for better debugging: String enums provide meaningful values that make debugging easier.

Next Article
TypeScript any Type

Y

yajasvikhqmsg
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • TypeScript
  • JavaScript-QnA
  • WebTech-FAQs

Similar Reads

  • TypeScript Enums Type
    TypeScript Enums Type is not natively supported by Javascript, but it's a notable feature of the Typescript Programming language. In general, enumerations shortly called "enum" are available in most programming languages to create named consonants. Enums allow us to create a set of named constants,
    5 min read
  • TypeScript Object
    A TypeScript object is a collection of key-value pairs, where keys are strings and values can be any data type. Objects in TypeScript can store various types, including primitives, arrays, and functions, providing a structured way to organize and manipulate data. What Are TypeScript Objects?An objec
    5 min read
  • How enums works in TypeScript ?
    In this article, we will try to understand all the facts which are associated with enums in TypeScript. TypeScript enum: TypeScript enums allow us to define or declare a set of named constants i.e. a collection of related values which could either be in the form of a string or number or any other da
    4 min read
  • Enums in JavaScript
    Enums in JavaScript are used to define a set of named constants and make your code more readable and easier to understand. Instead of using random numbers or strings, enums give meaningful names to values, helping you avoid errors and improve maintainability. They're a simple way to group related va
    4 min read
  • TypeScript any Type
    In TypeScript, any type is a dynamic type that can represent values of any data type. It allows for flexible typing but sacrifices type safety, as it lacks compile-time type checking, making it less recommended in strongly typed TypeScript code. It allows developers to specify types for variables, f
    4 min read
  • Opaque Types In TypeScript
    In TypeScript Opaque types concept allows for the definition of specialized types such as strings or numbers which are derived from primitive types but do not have the characteristics of the base types, the purpose of this is to prevent specific actions regarding the type in question, or to make thi
    4 min read
  • Data types in TypeScript
    In TypeScript, a data type defines the kind of values a variable can hold, ensuring type safety and enhancing code clarity. Primitive Types: Basic types like number, string, boolean, null, undefined, and symbol.Object Types: Complex structures including arrays, classes, interfaces, and functions.Pri
    3 min read
  • Interfaces in TypeScript
    TypeScript is a statically typed superset of JavaScript that adds optional types, classes, interfaces, and other features to help developers build robust and maintainable applications. One of the most powerful features of TypeScript is interfaces, which allow you to define the structure of objects,
    4 min read
  • TypeScript Enum to Object Array
    TypeScript enums allow us to define or declare a set of named constants i.e. a collection of related values which could either be in the form of a string or number or any other data type. To convert TypeScript enum to object array, we have multiple approaches. Example: enum DaysOfWeek { Sunday = 'SU
    4 min read
  • Hello World in TypeScript
    TypeScript is an open-source programming language. It is developed and maintained by Microsoft. TypeScript follows javascript syntactically but adds more features to it. It is a superset of javascript. The diagram below depicts the relationship: Typescript is purely object-oriented with features lik
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences