Creating Javascript Enum values

Updated on 29 Feb 2012,
Published on 22 Sep 2008

Javascript enum variable allows you to create a collection of integer based constant values. While working with Javascript variables instead of creating individual variable for constants you must use Javascript enum variable to store integer based values. Javascript enum is an enumeration type collection that stores the items with comma separation "," and its corresponding integer value separated with colon ":". Pair of item and integer value represents the variable with constant value. Javascript enum type variables and their values provide the systematic code pattern as well as readability and understanding for the code. You can easily get the integer value associated with the named enum type item.

JavaScript Basics Examples:

You can see the live samples and examples of JavaScript Basics from the following links:

Following are the different styles to create the Javascript enum type variables:

Example 1
var Days = {"sunday" : 0, "monday" : 1, "tuesday" : 3, "wednesday" : 4, "thursday" : 5, "friday" : 6, "saturday" : 7};

document.write(Days.friday);
Example 2
function Enum() {}

Enum.ColorType = {red:0, blue:1, green:2}

document.write(Enum.ColorType.blue);
Example 3
var enumObj = new Object();

enumObj.fontSize = {small:10, medium:12, large:14}

document.write(enumObj.fontSize.small);

You can also assign a new value to the enum variable e.g.:

enumObj.fontSize.small = 9;

Output:

You can see the output of above discussed codes from the following links:

JavaScript Enum

JavaScript Enumerator Object

Continue to next tutorial: JavaScript If-Else Statements to learn javascript basics of if-else conditions with examples.

3 Responses to "Creating Javascript Enum values"
Collin
Hey, Seems like the point of an enumeration is to create a set of acceptable values for a particular variable. One important part of enumerations is that they are always required/guaranteed to be one of those possible values. But the enums you show on this page do not provide that functionality. For example, if instead of typing "Days.friday", I type "Days.Friday", I will now have an undefined variable moving around in my code. Depending on how far it gets, debugging will be very painful. One way to make these enumerations a little safer would be to use functions to enumerate the possible values. For example: Days.monday = function() { return "monday"; }; You can use any unique return value for the enum function. Then you can call it like: document.write( Days.monday() ) or compare it like: if ( myDay === Days.monday() ) {...} This will give you a fail-fast enumeration that prints a nice error in your console if you mistype the enum value name. What do you think? Collin
How can I get the enum labels knowing the value. For example, using the enum: var Days = {"sunday" : 0, "monday" : 1, "tuesday" : 3, "wednesday" : 4, "thursday" : 5, "friday" : 6, "saturday" : 7}; How can I get the string tuesday, knowing the value 3?
you could use the 'for in' declaration to loop through members and return the one with a specified index something like like : Enum.prototype.byIndex = function(i){ var ii = 0; for(var lbl in this){ if(ii == i) return lbl; ii += 1; } }
Leave a Comment
* required
* required
* will not be published
* optional
* hint: http://www.example.com
  • Subscribe via Email