How to define Enum in JavaScript

Everybody familiar with Enum in server side scripting language.  I was wondering if we can define Enum type in JavaScript and I found that we can define type as Enum.

Here is the syntax to define Enum.

var Status =
{

    Failed:0;
    Succeeded:1;

}

Here is example how you can use it.

function DoAction(status)
{

    switch(Number(status)){

        case Status.Failed:
             // execute some code;
             break;

       case Status.Succeeded:
           //execute some code;
           break;
     }
}

Since every variable type we define in JavaScript is as “var” you can’t really take advantage of Enum type we define here.  You can still pass any type of values to method DoAction even though parameter type is Enum.

For example,

DoAction(3);  //method call
DoAction("SomeString");

Here you can see that we can pass 3 or “SomeString” to DoAction method parameter which is not valid enum type.  If you want to display message or want to execute some code in case of invalid value you can add another switch case.

function DoAction(status)
{

   switch(Number(status)){

        case Status.Failed:
             // execute some code;
            break;

       case Status.Succeeded:
           //execute some code;
          break;
 
       case NaN:
       default:
            //if string value is passed, Number("someString") will return NaN
            //execute some code if parameter value is not valid enum type
         break;
    }

}

Hope this helps.

Tagged with:
Posted in Microsoft Technology
One comment on “How to define Enum in JavaScript
  1. Mia says:

    Improbable submit! This might help a lot of folks find out about this matter.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 
Ads
OutbackDirect.co.uk is a trading site of World of Power.co.uk Limited. Outback Direct is the official Outback Consumer Warranty, Parts and Accessory Partners for the UK. We currently hold in stock 1000’s of parts and accessories for current and older Outback barbecues. We endeavour to ensure rapid and secure product delivery the length and breadth of mainland United Kingdom, this factor coupled with our excellent customer service has led to the market position we currently enjoy.
outback barbecue
omega 300 gas barbecue
Omega 250 Gas Hooded BBQ
------------------------------------------------------------ Silver Sky Imports is a unique online marketplace that offers unique handicrafts from Tibet and Nepal. Launched in 2003, Silver Sky Imports is currently the largest supplier of handmade items from the Himalayas. We specialize in the largest variety of Tibetan Singing Bowls and Crystal Singing Bowls online and include individual sound samples, pictures and descriptions for each bowl. Some of our other products include Tibetan Prayer Flags, Tingshas, Bell and Dorje, Incense, Felted Wool Handbags, Wood Masks, Prayer Wheels, Gongs, Bronze Statues, and Tibetan Meditation Rugs. monks
------------------------------------------------------------ Airpush, a mobile ad network that is only two years old, has quickly grown to one with over 5,000 advertisers. One thing they've heard from that sea of advertisers over the past 12 months, and especially over the past six, is that real-time bidding (RTB) capabilities are now a must. To answer those needs, the company today unveiled AirDSP, a platform meant to easily connect advertisers to RTB exchanges. Read more: Mobile RTB
------------------------------------------------------------ Danish site about electronic cigarettes E Cigarette, Electronic Cigarette and E Cigarettes e cigaret
------------------------------------------------------------ DrugRisks.com drugrisk​ recall ​informat​ion​ drugrisk​ yaz​
------------------------------------------------------------