In this article, i will tell you how to declare enum in javascript.
var Direction =
{
North = 90,
East = 180,
South = 270,
West = 360
}
Create a function in which we will define enum
function Display(Direction_) {
switch (Number(Direction_)) {
case Direction.North:
alert("Direction North, Value:" + Direction.North);
break;
case Direction.East:
alert("Direction North, Value:" + Direction.East);
break;
case Direction.South:
alert("Direction North, Value:"+ Direction.South);
break;
case Direction.West:
alert("Direction North, Value:" + Direction.West);
break;
}
}
Now call the function and pass the parameters.
Display(90);
Display(180);
Display(270);
Display(360);
Display(Direction.North);
Display(Direction.East);
Display(Direction.South);
Display(Direction.West);
Here is the simple example to declare enum instead of using if,if else, else, if there are many if else conditions.