![]() |
TypeScript Enums is a feature that lets you define a group of named constants, which can help you manage and understand your code more easily. There are two types of Enums in TypeScript:
Numeric Enums: DefaultNumeric enums are the default in TypeScript. Enums will assign the first value to 0 and then add 1 to every additional value. Example: In this example, Direction.Up is explicitly set to 0. The following values(Down, Left, Right) are automatically assigned incremental values (1,2,3).
Output: 1 Numeric Enums: InitializedWe can set the value of the first enum and the value of the other enums auto increment from that. Example: In this example, we set the value of the Direction.Up to 1. The following enums (Down, Left, Right) are automatically assigned incremental values (2, 3, 4).
Output: 1 Numeric Enums: Fully InitializedWe can assign unique values to every enum value. Then the values will not get incremented automatically. Example: In this example, we assign values to every enum.
Output: 1 String EnumsString Enums are like Numeric strings, but the value assigned to them is in strings. Example: In this example, we assign a string value to the Enums.
Output: "UP" |
Reffered: https://www.geeksforgeeks.org
JavaScript |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |