Enums in JavaScript
Tags: javascript, webdev, codequality, tutorial
Published: 2021-04-27 22:53:20

# Prologue

## A few words about me
My name is [Marcos Molina](https://dev.to/marcosmol204/enums-in-javascript-1274), a passionate Web Developer based in Israel.

## What is my goal?
Sharing knowledge, the purpose of each of my posts is to share a solution to a real problem I faced in the industry, side projects, or open-source contributions. 

## Why?
Because seeing real examples always have helped me more than "To-Do List" examples.

# Content

##Enums?

They are a data type that allows us to specify a list of values for a type.
In my side project "Askii" that allows users to answer questions, send it to their friend and see all the answers I used enums for "type" all the possible kinds of questions. 
Binary question: Yes or No.
Numeric questions: 1 ,2, etc.
Text questions: Yes, I do love coffee.

##What they do?

They force you to think about all the possible values that a variable can take.
Once I create an enum on my project, I know that I need to handle three different scenarios in the front-end and in the back-end: binary, numeric, and text questions.

##Enums vs list of numbers.

Enums are a constant rather than a list of numbers, increasing the readability of the code. 
*This point will be demonstrated in the code example.

##When should we use enums?
We should use enums if there are a definite number of fixed values for a variable. 

##How enums can be implemented?
JavaScript doesn't support enums "built-in", therefore there are some ways to implement them.

Let's build the solution
*Note: by convention we use Uppercase letters, written 'binary' but should be 'BINARY'*
![Alt text of image](https://media-exp1.licdn.com/dms/image/C4E22AQGDMomDl4V0lw/feedshare-shrink_2048_1536/0/1614451410589?e=1622678400&v=beta&t=-XMbNn1erNPtlSgxFL5Un_bCOfzuxJwNSblZCtUU3Dg)

**Edit 1:**
[Jon Randi](https://dev.to/jonrandy) wrote and I think he is right: 
"Might be better to just do"**
```javascript
const checkAnswerType = type => answerTypeEnum.hasOwnProperty(type)
```  
**Edit 2:**
[mao.zheng](https://dev.to/zhengmao) wrote that he prefers the next syntax:
```javascript
const Color = Object.freeze({
    RED  :  Symbol("red"),
    BLUE :  Symbol("blue"),
    GREEN:  Symbol("green")
}); 
```
**Why?**
>"Because this will force you to use the enum object defined above. you can't just use string(number) literal to make a comparison. It keeps the consistency of the codes your wrote"  

**Edit 3:**
It is also possible to use numbers as values, actually is the default values in others programming languages.

```javascript
const IssueStatusesEnum= Object.freeze({
    OPEN :  0,
    CLOSED:  1
}); 
```


# Summary
With the presented solution, the power of enums can be achieved as there is in some programming languages. For example TypeScript, Java, and C#.

I hope I could share with you some knowledge.
Did you learn something new ? Let me know in the comments. ❤️
Do you know another solution? 👆🏽
Did you love ? Share it with your friends. 👏🏽
Don't be afraid to post your thoughts. I'm here to learn from you. 😇
Networking? [LinkedIn](https://www.linkedin.com/in/marcos-mol/) 🤝🏽

```javascript
const moodsEnum = Object.freeze({ 
  GOOD : 'GOOD',
  AMAZING : 'AMAZING',
  SATISFACTORY: 'SATISFACTORY',
  ...
}
```
Thank you for reading.
Have an moodsEnum.AMAZING day!