In this article I will explain with an example, how to use switch case with Enum (Enumeration) in JavaScript.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type = "text/javascript">
        var colors = { "Red": 1, "Green": 2, "Blue": 3 };
        function DropdownChange(d) {
            switch (parseInt(d.value)) {
                case colors.Red:
                    alert("Red");
                    break;
                case colors.Green:
                    alert("Green");
                    break;
                case colors.Blue:
                    alert("Blue");
                    break;
            }
        }
    </script>
</head>
<body>
    <form>
        <select onchange = "DropdownChange(this)">
            <option value = "1">Red Color</option>
            <option value = "2">Green Color</option>
            <option value = "3">Blue Color</option>
        </select>
    </form>
</body>
</html>
 
In the above example I have created a simple enum of 3 colors Red, Green and Blue to which I have assigned integer values 1, 2 and 3 respectively. Now on the change of the HTML SELECT dropdown the function DropdownChange is called which based on the selected value displays the selected color in Alert using Switch case statement in JavaScript.
Note: Since by default JavaScript does not have any Enum data type nor it has type safety hence one can easily set any value to the Enums and modify them.
 
Screenshots
Using Switch Case with Enum in JavaScript
 
 
Demo
 
 
Downloads