Horje
Explain the concept of enums in TypeScript ?

Enum in TypeScript is a shorthand used for the enumerations. Enum is a way of defining multiple related named constants at the same place in the form of a collection which can be later accessed and assigned to other variables in the code. By default, the enums are assigned with number type values starting from 0. You can also assign them with the string values using the assignment operator(=). It helps to enhance the code readability and maintainability by defining meaningful identifiers for the magic strings and the numbers.

Syntax:

enum enumName {
// Assigned with default number value 0
property1,
// Assigned with the given string value
property2 = "stringValue",
}

Exmaple:

JavaScript
enum Direction {
    Up,
    Down,
    Left,
    Right
}

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

Output:

0



Reffered: https://www.geeksforgeeks.org


JavaScript

Related
How to handle asynchronous operation in TypeScript? How to handle asynchronous operation in TypeScript?
What is the never type in TypeScript? What is the never type in TypeScript?
How to use generics in TypeScript? How to use generics in TypeScript?
Explain the use of the readonly modifier in TypeScript ? Explain the use of the readonly modifier in TypeScript ?
How to Declare Optional Properties in TypeScript ? How to Declare Optional Properties in TypeScript ?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
12