Anyone that's used a programming language like Java, C#, Swift etc has most likely made use of enumerations. Enumeration is a versatile data structure that defines a set of named values a data set can contain. The named set of values makes code easier to read and allows for some elegant solutions to problems.
For example, a days of the week enumeration data type would contain the values Monday, Tuesday, Wednesday, Thursday, Friday, Saturday and Sunday.
Up until recently it wasn't possible to implement enumerations in JavaScript, it wasn't until JSON (JavaScript Object Notation) came along that it became possible to implement and use enumerations in a manner similar to the programming languages named in the opening sentence of this article.
So how can one implement enumeration in JavaScript? Let's use the days of the week as an example.
You could define a days of the week enumeration like this:
With the constant variable weekDays assigned the days of the week in a JSON data format we have created a rudimentary enumeration implementation that can be used in code as follows:
In the code above the variable selectedDay is set to weekDays.Saturday, when the switch statement runs the message 'Selected day is Saturday' is displayed in the console window as the condition case weekDays.Saturday is met.
Next we'll use enumerations in a more real life client side senario to update a web pages presentation.
Okay lets use enumeration in a real world senario instead of presenting code snippets that at times make it hard to think of when you might apply the technique. In this example we're going to simulate a user filling out a form, before the user fills out the form they will be presented with some introductory text and when the form is completed the page will display a message that the form has been completed.
In this example the content displayed is updated without refreshing the page, click on the next button to move through the screens:
Introduction
Some introductory text goes here. Click next to continue
Questions
This would be the questions page.
Completed
Display some completion message.
Click on the next button to move to the next screen and on the final screen click on the "Restart" button to reset the demonstration to the start screen.
The above example though trivial contains all the componets and mechanisms that you would find in a commercial web application and can be easily expanded to include multiple screens. Below is a listing of the html and JavaSript code of the screen example above.
The above JavaScript code has the following execution sequence:
When the web page loads the initialise function is called, this function initialises variables and then ends by calling the updateDisplay function
When the user first clicks on the 'Next' button the function introductionOnClick is called that calls the function setDisplay passing the parameter value 'screens.Questions'
The setDisplay function sets the currentDisplay variable to the parameter value 'screens.Questions' and then calls the updateDisplay function
The updateDisplay function first calls hideAll to hide all the div elements and then sets screenQuestions.style.display = '';, this action make the question screen div element appear
On the questions screen when the user clicks on the 'Next' button the same sequence is repeated again as in steps 2, 3 and 4 except the complete screen is displayed as 'screens.Complete' is passed to the updateDisplay function
On the completed screen when the user clicks on the 'Restart' button 'screens.Introduction' is passed to the updateDisplay function which leads to the introduction screen being displayed
Enumeration is a good way of keeping track of a users progress through a series of screens and it makes code more readable than declaring multiple variables for each value in a data set. After implementing code that makes use of enumeration you'll start to use it more often and wonder what you did before discovering this data type.