Creating Javascript Enum values
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:
Continue to next tutorial: JavaScript If-Else Statements to learn javascript basics of if-else conditions with examples.
