Introduction:
In this article i am going to explain about how to declare and use application wide constants (enum’s) in javascript.
Main:
What is enum?
The enum keyword is used to declare an enumeration, a distinct type consisting of a set of named constants called the
enumerator list. Every enumeration type has an underlying type, which can be any integral type except char. The default
underlying type of the enumeration elements is int. By default, the first enumerator has the value 0, and the value of
each successive enumerator is increased by 1.
enum Days {Sat, Sun, Mon, Tue, Wed, Thu, Fri};
In javascript just declare like below,
var val =
{
False:0;
True:1;
}
var val = { False:0; True:1; } |
How to use?
enum Days {Sat=1, Sun, Mon, Tue, Wed, Thu, Fri};
static void Main()
{
int x = (int)Days.Sun;
int y = (int)Days.Fri;
Console.WriteLine("Sun = {0}", x);
Console.WriteLine("Fri = {0}", y);
}
enum Days {Sat=1, Sun, Mon, Tue, Wed, Thu, Fri}; static void Main() { int x = (int)Days.Sun; int y = (int)Days.Fri; Console.WriteLine("Sun = {0}", x); Console.WriteLine("Fri = {0}", y); } |
Same like javascript,
function Enumin(val)
{
switch(Number(val)){
case Status.False:
alert('I am false');
break;
case Status.True:
alert('I am true');
break;
}
}
function Enumin(val) { switch(Number(val)){ case Status.False: alert('I am false'); break; case Status.True: alert('I am true'); break; } } |
Thats it!
Conclusion:
Hope this helps,
Happy coding.