πŸ“˜
NavDoc by Bash School
GithubContact
πŸ“˜
NavDoc by Bash School
  • πŸŽ“Introduction
  • 🐒Getting Started
  • ⚑Changelog
  • πŸ‘¨β€πŸš€Maintainers
  • πŸ›£οΈRoadmap
  • Fundamentals
    • The Internet
      • Introduction
      • What is a URL
      • What is a port
      • The DNS protocol
      • The TCP protocol
      • The UDP protocol
      • The Web
      • The HTTP protocol
      • Hyperlinks
      • What is a Web browser
      • What is a Web server
    • HTML
      • Your first HTML page
      • Text tags
      • Attributes
      • Links
      • Images
      • Lists
      • Head Tags
      • Container tags
    • CSS
      • Introduction
      • Colors
      • selectors
      • Cascade
      • Specificity
      • Units
      • Advanced selectors
      • Typography
      • The box model
      • The display property
      • Responsive design
  • JavaScript
    • Basics
      • Introduction
      • Literals , Identifiers, Variables
      • Comments
      • The difference between let, const and var
      • Types
      • Operators and expressions
      • Arithmetic operators
      • The assignment operator
      • Operators precedence
      • Strings
      • Numbers
      • Semicolons, white space and sensitivity
      • Arrays
      • Conditionals
      • Loops
      • Functions
      • Objects
      • Arrays + functions
      • OOPS
      • Asynchronous
      • Scope, hoisting, event loop
      • ES Modules
      • Errors and exceptions
      • Built-in objects
        • The global object
        • Object properties
        • Number
        • String
        • Math
        • JSON
        • Date
        • Intl
        • Set and Map
      • More operators
    • Nodejs
      • Getting Started
      • Installation
      • Hello World in Node
      • Modules
      • Packages
      • File Handling
      • HTTP Request
      • Processing Files
      • HTTP
    • Express.js
      • Getting Started
      • Middleware
      • Serve Static Assets
      • How to Send Files to the Client
      • Sessions
      • Validate Input
      • Sanitizing Data
      • Forms
      • File Uploads
    • React
      • Setting up a React project with Vite
      • React Components
      • Introduction to JSX
      • Using JSX to compose UI
      • The difference between JSX and HTML
      • Embedding JavaScript in JSX
      • Handling user events
      • Managing state
      • Component props
      • Data flow
      • Lifecycle events
      • Managing forms in React
      • Install the React Developer Tools
      • Installing Tailwind CSS in a React app
      • Build a counter in React
    • TypeScript
      • Key Benefits
      • Types of Languages
      • The Need for TypeScript
      • What is TypeScript?
      • The tsc Compiler
      • Basic Types in TypeScript
      • tsconfig
      • Interfaces
      • Types
      • Arrays in TypeScript
      • Enums
      • Exporting and importing
    • MongoDB
      • SQL vs. NoSQL Databases
      • Installing MongoDB
      • MongoDB Databases and Collections
      • Working with Documents
      • MongoDB Operators
      • Sorting, Indexing & Searching
      • Built-in Methods
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub
  1. JavaScript
  2. TypeScript

Enums

Enums (short for enumerations) in TypeScript are a feature that allows you to define a set of named constants. They provide a human-readable way to represent a set of constant values, which might otherwise be represented as numbers or strings.

Example 1: Game Controls

Let’s say you have a game where you have to perform an action based on whether the user has pressed the up, down, left, or right arrow key.

enum Direction {
    Up,
    Down,
    Left,
    Right
}

function doSomething(keyPressed: Direction) {
    // do something.
}

doSomething(Direction.Up);

This makes the code cleaner and easier to understand. At runtime, the values stored are still numbers (0, 1, 2, 3).

2. Runtime Values of Enums

Let's log the runtime value of Direction.Up:

console.log(Direction.Up); // Output: 0

By default, enums get values starting from 0 and incrementing by 1 for each subsequent member.

3. Customizing Enum Values

You can customize the values of enums:

enum Direction {
    Up = 1,
    Down,   // 2
    Left,   // 3
    Right   // 4
}

function doSomething(keyPressed: Direction) {
    // do something.
}

doSomething(Direction.Down);

4. Using String Values

Enums can also be represented as strings:

enum Direction {
    Up = "UP",
    Down = "Down",
    Left = "Left",
    Right = "Right"
}

function doSomething(keyPressed: Direction) {
    // do something.
}

doSomething(Direction.Down);

5. Common Use Case in Express

Enums are commonly used in frameworks like Express to represent status codes:

enum ResponseStatus {
    Success = 200,
    NotFound = 404,
    Error = 500
}

app.get("/", (req, res) => {
    if (!req.query.userId) {
        res.status(ResponseStatus.Error).json({});
    }
    // and so on...
    res.status(ResponseStatus.Success).json({});
});

Enums provide a convenient way to define a set of related constants and improve code readability and maintainability. They are widely used in various scenarios, including game development, web frameworks, and API development.

PreviousArrays in TypeScriptNextExporting and importing

Last updated 12 months ago

Was this helpful?