Saturday, October 27, 2018

TypeScript - DataTypes - Enums

enums are a feature provided by TypeScript that make numbers more expressive.

Consider a case you have a number of food items and each food item is represented by a number. Then you need to see which food item was chosen using a switch case statement. You can hard-code this in your program as say:
101 - Vada Pav
121 - Bhaji Pav
212 - Misal Pav
312 - Bhurji Pav
434 - Usal Pav

But, computer does not care about this. You could have 0,1,2,3,4 ... etc ,, each number representing one food item. This will normalize this and would be good thing. But it makes it harder to remember which food the number 3 stands for. Does 3 stands misal Pav or Bhurji Pav?

To make this type of scenarios easier, we can use an enum as follows. Consider the file enumFile.ts with the following content:

enum foodItem{
VadaPav,
BhajiPav,
MisalPav,
BhurjiPav,
UsalPav
}

let myVadaPav:foodItem = foodItem.VadaPav;
console.log("enum value of myVadaPav = "+myVadaPav);

let myBhajiPav:foodItem = foodItem.BhajiPav;
console.log("enum value of myBhajiPav = "+myBhajiPav);

let myMisalPav:foodItem = foodItem.MisalPav;
console.log("enum value of myMisalPav = "+myMisalPav);

let myBhurjiPav:foodItem = foodItem.BhurjiPav;
console.log("enum value of myBhurjiPav = "+myBhurjiPav);

let myUsalPav:foodItem = foodItem.UsalPav;
console.log("enum value of myUsalPav = "+myUsalPav);

Now let us compile the file:

$ tsc enumFile.ts
$

Now, In the browser console, we can see the following output:

enumFile.js:10 enum value of myVadaPav = 0
enumFile.js:12 enum value of myBhajiPav = 1
enumFile.js:14 enum value of myMisalPav = 2
enumFile.js:16 enum value of myBhurjiPav = 3
enumFile.js:18 enum value of myUsalPav = 4

We don't see the name of the food items but only the numbers in the console. This is because behind the scenes numbers are assigned to the food items automatically.

enum foodItem{
VadaPav, // 0
BhajiPav, // 1
MisalPav, // 2
BhurjiPav, // 3
UsalPav // 4
}

The first food item has received the number 0
the second food item has received the number 1 and so on...

Now, there could be some applications where we want to overwrite the default values created by enum. Say, instead at starting at 0, I want to start at 1000. For this, let us just assign the first value in enum to 1000.

enum foodItem{
VadaPav = 1000, // 0
BhajiPav, // 1
MisalPav, // 2
BhurjiPav, // 3
UsalPav // 4
}

let myVadaPav:foodItem = foodItem.VadaPav;
console.log("enum value of myVadaPav = "+myVadaPav);

let myBhajiPav:foodItem = foodItem.BhajiPav;
console.log("enum value of myBhajiPav = "+myBhajiPav);

let myMisalPav:foodItem = foodItem.MisalPav;
console.log("enum value of myMisalPav = "+myMisalPav);

let myBhurjiPav:foodItem = foodItem.BhurjiPav;
console.log("enum value of myBhurjiPav = "+myBhurjiPav);

let myUsalPav:foodItem = foodItem.UsalPav;
console.log("enum value of myUsalPav = "+myUsalPav);

Console Output:

enumFile.js:10 enum value of myVadaPav = 1000
enumFile.js:12 enum value of myBhajiPav = 1001
enumFile.js:14 enum value of myMisalPav = 1002
enumFile.js:16 enum value of myBhurjiPav = 1003
enumFile.js:18 enum value of myUsalPav = 1004


This gives us the idea to modify enums.


Reference:



No comments:

Post a Comment