JavaScript Enum | Typescript Enum Tutorial By WDH

JavaScript Enum


JavaScript Enum
Enumerated values
Sometimes you want a variable that can take only a certain listed(enumerated) set of values. The values are constants as opposed to types like Number or String which can have a wide range of values.
This is useful for many situations: For example, when describing in temperate climates of days, you’ll want to limit the options to certain possible values: Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday and Sunday.
Let’s look at different ways of implementing this type of data:
Enum Implementation
While this would work for small codebases, we will face a few immediate issues:
  1. It’s easy to make mistakes in your code. A developer can make the mistake of using integers outside the range of the ones defined.
  2. Definitions from unrelated enums can overlap and cause conflicts:
Example
Output

     3.This is semantically incorrect - Temperatures are really integers or strings, they’re days!

Another example of enum
Output