Throw new Error("Color is not there in the above enumEnum Constant with Colors") declaring enum constant with colors variable Throw new Error("Direction is not there in the above enumHi, I am EAST ") Ĭhange the SOUTH value of direction into HELLO in Line1. ![]() ![]() Accessing single value to check with switch case declaring enum constant with directions variable How does Enum work in JavaScript?Įnum constant in JavaScript can be worked with “const” keyword and this constants are always specified in capital letters only.īelow are the examples mentioned: Example #1 Now you can see in the above there is no chance to make any mistake because all constant always defined with capital letters only. Throw new Error('Direction is not there in the above enum') Enum Constant with ObjectsĪ better way to implement enum constant is by using object, lets see the below example. Now we will look into the above directions example with enum concept in JavaScript and see the efficiency. As it is very easy to fix the problem because we have only just few lines of code, let’s imagine if we have thousands of lines of code then it is really hard to fix the problem. If you observe carefully the above code then the problem with above implementation is highlighted in the above example that would be fail in case of “west” since we made spelling error in our switch-case statement but by mistake we have used the case as “West” instead. ![]() Let’s thought you don’t know about enums implementation in JavaScript then your conventional approach for this directions is same like in the first example summer scenario. Now, let’s consider the same scenario in different way like if in our application we have 4 values for directions (EAST, WEST, NORTH, SOUTH).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |