{"id":23158,"date":"2018-11-09T16:15:21","date_gmt":"2018-11-09T14:15:21","guid":{"rendered":"http:\/\/www.webcodegeeks.com\/?p=23158"},"modified":"2018-11-09T11:50:11","modified_gmt":"2018-11-09T09:50:11","slug":"javascript-enums-example","status":"publish","type":"post","link":"https:\/\/www.webcodegeeks.com\/javascript\/javascript-enums-example\/","title":{"rendered":"JavaScript Enums Example"},"content":{"rendered":"<p>Greetings readers, in this tutorial, we will show how to use the JavaScript <strong>enumerations<\/strong>.<\/p>\n<h2>1. Introduction<\/h2>\n<p><strong>JavaScript<\/strong> is an object-oriented programming language that allows the <em>client-side scripting<\/em> to interact with a user and deliver the dynamic pages. Most web browsers including Google Chrome, Mozilla Firefox, Safari, Internet Explorer, Microsoft Edge, Opera, etc. support it. The JavaScript scripting language includes:<\/p>\n<ul>\n<li>Declaring variables<\/li>\n<li>Maintaining the retrieving values<\/li>\n<li>Defining and invoking functions<\/li>\n<li>Defining classes<\/li>\n<li>Load and use external modules<\/li>\n<li>Define event handlers<\/li>\n<li>And much more \u2026.<\/li>\n<\/ul>\n<h3>1.1 Advantages of JavaScript Language<\/h3>\n<p>The pros of using the JavaScript scripting language are:<\/p>\n<ul>\n<li>JavaScript is easy to learn<\/li>\n<li>It executes on client&#8217;s browser, so eliminates the server-side processing and be executed on any OS<\/li>\n<li>JavaScript can be used with any type of web page e.g. PHP, ASP.NET, Perl etc<\/li>\n<li>Web-page performance increases due to client-side execution<\/li>\n<li>JavaScript code can be minified to decrease the loading time from the server<\/li>\n<li>Many JavaScript-based application frameworks are available in the market to create Single page web applications e.g. AngularJS, ReactJS etc.<\/li>\n<\/ul>\n<p>[ulp id=&#8217;tCIwOngQUb3zSUuF&#8217;]<\/p>\n<h3>1.2 Disadvantages of JavaScript Language<\/h3>\n<p>The cons of using the JavaScript scripting language are:<\/p>\n<ul>\n<li>No support for networking applications<\/li>\n<li>Does not have any multi-threading or multi-processing capabilities<\/li>\n<li>Does not allow the file reading and writing capabilities<\/li>\n<\/ul>\n<h2>1.3 JavaScript Enum<\/h2>\n<ul>\n<li>Enum represents a fixed number of values (also known as <em>constants<\/em>) in a programming language<\/li>\n<li>Offers,\n<ul>\n<li>Consistency as developers does not worry about the exact value of the string<\/li>\n<li>Excellent exception handling<\/li>\n<\/ul>\n<\/li>\n<li>Makes the code more stable, easy to read and less error-prone<\/li>\n<li>Syntax: <code>enum WeekDay = { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY };<\/code><\/li>\n<\/ul>\n<h2>2. JavaScript Enums Example<\/h2>\n<p>Here is a systematic guide for implementing this tutorial using the JavaScript language.<\/p>\n<h3>2.1 Tools Used<\/h3>\n<p>We are using Eclipse Kepler SR2, JDK 8 and Maven. Having said that, we have tested the code against JDK 1.7 and it works well.<\/p>\n<h3>2.2 Project Structure<\/h3>\n<p>Firstly, let us review the final project structure if you are confused about where you should create the corresponding files or folder later!<\/p>\n<figure id=\"attachment_23159\" aria-describedby=\"caption-attachment-23159\" style=\"width: 324px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2018\/11\/structure-img1.jpg\"><img decoding=\"async\" class=\"wp-image-23159 size-full\" src=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2018\/11\/structure-img1.jpg\" alt=\"JavaScript Enums - Application Project Structure\" width=\"324\" height=\"250\" srcset=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2018\/11\/structure-img1.jpg 324w, https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2018\/11\/structure-img1-300x231.jpg 300w\" sizes=\"(max-width: 324px) 100vw, 324px\" \/><\/a><figcaption id=\"caption-attachment-23159\" class=\"wp-caption-text\">Fig. 1: Application Project Structure<\/figcaption><\/figure>\n<h3>2.3 Project Creation<\/h3>\n<p>This section will show on how to create a Java-based Maven project with Eclipse. In Eclipse Ide, go to <code>File -&gt; New -&gt; Maven Project<\/code>.<\/p>\n<figure id=\"attachment_23160\" aria-describedby=\"caption-attachment-23160\" style=\"width: 657px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2018\/11\/js_enum_project_guide_1.jpg\"><img decoding=\"async\" class=\"wp-image-23160 size-full\" src=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2018\/11\/js_enum_project_guide_1.jpg\" alt=\"JavaScript Enums - Create a Maven Project\" width=\"657\" height=\"690\" srcset=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2018\/11\/js_enum_project_guide_1.jpg 657w, https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2018\/11\/js_enum_project_guide_1-286x300.jpg 286w\" sizes=\"(max-width: 657px) 100vw, 657px\" \/><\/a><figcaption id=\"caption-attachment-23160\" class=\"wp-caption-text\">Fig. 2: Create a Maven Project<\/figcaption><\/figure>\n<p>In the New Maven Project window, it will ask you to select the project location. By default, &#8216;Use default workspace location&#8217; will be selected. Just click on the next button to proceed.<\/p>\n<figure id=\"attachment_23161\" aria-describedby=\"caption-attachment-23161\" style=\"width: 598px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2018\/11\/js_enum_project_guide_2.jpg\"><img decoding=\"async\" class=\"wp-image-23161 size-full\" src=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2018\/11\/js_enum_project_guide_2.jpg\" alt=\"JavaScript Enums - Project Details\" width=\"598\" height=\"543\" srcset=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2018\/11\/js_enum_project_guide_2.jpg 598w, https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2018\/11\/js_enum_project_guide_2-300x272.jpg 300w\" sizes=\"(max-width: 598px) 100vw, 598px\" \/><\/a><figcaption id=\"caption-attachment-23161\" class=\"wp-caption-text\">Fig. 3: Project Details<\/figcaption><\/figure>\n<p>Select the &#8216;Maven Web App&#8217; Archetype from the list of options and click next.<\/p>\n<figure id=\"attachment_23162\" aria-describedby=\"caption-attachment-23162\" style=\"width: 515px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2018\/11\/js_enum_project_guide_3.jpg\"><img decoding=\"async\" class=\"wp-image-23162 size-full\" src=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2018\/11\/js_enum_project_guide_3.jpg\" alt=\"JavaScript Enums - Archetype Selection\" width=\"515\" height=\"541\" srcset=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2018\/11\/js_enum_project_guide_3.jpg 515w, https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2018\/11\/js_enum_project_guide_3-286x300.jpg 286w\" sizes=\"(max-width: 515px) 100vw, 515px\" \/><\/a><figcaption id=\"caption-attachment-23162\" class=\"wp-caption-text\">Fig. 4: Archetype Selection<\/figcaption><\/figure>\n<p>It will ask you to &#8216;Enter the group and the artifact id for the project&#8217;. We will input the details as shown in the below image. The version number will be by default: <code>0.0.1-SNAPSHOT<\/code>.<\/p>\n<figure id=\"attachment_23163\" aria-describedby=\"caption-attachment-23163\" style=\"width: 598px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2018\/11\/js_enum_project_guide_4.jpg\"><img decoding=\"async\" class=\"wp-image-23163 size-full\" src=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2018\/11\/js_enum_project_guide_4.jpg\" alt=\"JavaScript Enums - Archetype Parameters\" width=\"598\" height=\"536\" srcset=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2018\/11\/js_enum_project_guide_4.jpg 598w, https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2018\/11\/js_enum_project_guide_4-300x269.jpg 300w\" sizes=\"(max-width: 598px) 100vw, 598px\" \/><\/a><figcaption id=\"caption-attachment-23163\" class=\"wp-caption-text\">Fig. 5: Archetype Parameters<\/figcaption><\/figure>\n<p>Click on Finish and the creation of a maven project is completed. If you see, it has downloaded the maven dependencies and a <code>pom.xml<\/code> file will be created. It will have the following code:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>pom.xml<\/em><\/span><\/p>\n<pre class=\"brush:xml; wrap-lines:false;\">&lt;project xmlns=\"http:\/\/maven.apache.org\/POM\/4.0.0\" xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:schemaLocation=\"http:\/\/maven.apache.org\/POM\/4.0.0 http:\/\/maven.apache.org\/xsd\/maven-4.0.0.xsd\"&gt;\r\n\t&lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt;\r\n\t&lt;groupId&gt;com.javascript&lt;\/groupId&gt;\r\n\t&lt;artifactId&gt;JavaScriptEnum&lt;\/artifactId&gt;\r\n\t&lt;version&gt;0.0.1-SNAPSHOT&lt;\/version&gt;\r\n\t&lt;packaging&gt;war&lt;\/packaging&gt;\r\n&lt;\/project&gt;\r\n<\/pre>\n<h2>3. Application Building<\/h2>\n<p>Let us create an application to understand the basic building blocks of this tutorial.<\/p>\n<h3>3.1 Define the HTML<\/h3>\n<p>Let us write a simple index page in the <code>JavaScriptEnum\/src\/main\/webapp\/<\/code> folder. Add the following code to it:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>index.jsp<\/em><\/span><\/p>\n<pre class=\"brush:html; wrap-lines:false;\">&lt;!------ JavaScript Enums Example ------&gt;\r\n&lt;div class=\"form-group\"&gt;\r\n\t&lt;!-- What is Enum? --&gt;\r\n\t&lt;div id=\"enum-info-result\"&gt;\r\n\t\t&lt;h4 class=\"text-primary font-weight-bold\"&gt;Enums&lt;\/h4&gt;\r\n\t\t&lt;ol class=\"text-info line-hgt\"&gt;\r\n\t\t\t&lt;li&gt;In a programming language, an enumerated type (also called &lt;em&gt;enumeration&lt;\/em&gt; or &lt;em&gt;enum&lt;\/em&gt;) is a data-type consisting of a set of named values considered  elements.&lt;\/li&gt;\r\n\t\t\t&lt;li&gt;They behave like &lt;strong&gt;constants&lt;\/strong&gt; in a programming language.&lt;\/li&gt;\r\n\t\t\t&lt;li&gt;E.g. &lt;pre&gt;enum WeekDay = { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY };&lt;\/pre&gt;&lt;\/li&gt;\r\n\t\t&lt;\/ol&gt;\r\n\t&lt;\/div&gt;\r\n\t&lt;!-- Direction selection drop-down --&gt;\r\n\t&lt;div id=\"code\"&gt;\r\n\t\t&lt;h4 class=\"text-sucess\"&gt;Code Snippet&lt;\/h4&gt;&lt;div&gt;\u00a0&lt;\/div&gt;\r\n\t\t&lt;div class=\"col-sm-4\"&gt;\r\n\t\t\t&lt;select id=\"dir_idx\" class=\"form-control\" onchange=\"sayDirection();\"&gt;\r\n\t\t\t\t&lt;option selected=\"selected\" value=\"-1\" disabled&gt;Select direction&lt;\/option&gt;\r\n\t\t\t\t&lt;option value=\"UP\"&gt;Up&lt;\/option&gt;\r\n\t\t\t\t&lt;option value=\"DOWN\"&gt;Down&lt;\/option&gt;\r\n\t\t\t\t&lt;option value=\"LEFT\"&gt;Left&lt;\/option&gt;\r\n\t\t\t\t&lt;option value=\"RIGHT\"&gt;Right&lt;\/option&gt;\r\n\t\t\t&lt;\/select&gt;\r\n\t\t&lt;\/div&gt;\r\n\t&lt;\/div&gt;\r\n&lt;\/div&gt;\r\n&lt;div&gt;\u00a0&lt;\/div&gt;\r\n\r\n&lt;!-- Result --&gt;\r\n&lt;h5 id=\"result\" class=\"text-info\"&gt;&lt;\/h5&gt;\r\n<\/pre>\n<h3>3.2 Define the JavaScript function<\/h3>\n<p>Let us write a simple javascript function that shows the <span style=\"text-decoration: underline;\">enum<\/span> implementation in the javascript language. Add the following code to it:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>JavaScript function<\/em><\/span><\/p>\n<pre class=\"brush:js; wrap-lines:false;\">\/\/ Declaring enum\r\nconst Direction = {\r\n\tUP: 'UP',\r\n\tDOWN: 'DOWN',\r\n\tLEFT: 'LEFT',\r\n\tRIGHT: 'RIGHT'\r\n};\r\n\r\nfunction sayDirection() {\r\n\tvar direction = document.getElementById(\"dir_idx\").value;\r\n\tswitch (direction) {\r\n\t\tcase Direction.UP:\r\n\t\t\tdocument.getElementById(\"result\").innerHTML = 'We are going up!';\r\n\t\t\tbreak;\r\n\t\tcase Direction.DOWN:\r\n\t\t\tdocument.getElementById(\"result\").innerHTML = 'Heading all the way down. . .';\r\n\t\t\tbreak;\r\n\t\tcase Direction.LEFT:\r\n\t\t\tdocument.getElementById(\"result\").innerHTML = 'Left not right!';\r\n\t\t\tbreak;\r\n\t\tcase Direction.RIGHT:\r\n\t\t\tdocument.getElementById(\"result\").innerHTML = 'To the right.';\r\n\t\t\tbreak;\r\n\t}\r\n}\r\n<\/pre>\n<h3>3.3 First application<\/h3>\n<p>Complete the above steps and save the file. Let us see the sample code snippet.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>index.jsp<\/em><\/span><\/p>\n<pre class=\"brush: html; wrap-lines:false;\">&lt;!DOCTYPE html PUBLIC \"-\/\/W3C\/\/DTD HTML 4.01 Transitional\/\/EN\" \"http:\/\/www.w3.org\/TR\/html4\/loose.dtd\"&gt;\r\n&lt;html&gt;\r\n\t&lt;head&gt;\r\n\t\t&lt;title&gt;Index page&lt;\/title&gt;\r\n\t\t&lt;meta http-equiv=\"Content-Type\" content=\"text\/html; charset=ISO-8859-1\"&gt;\r\n\t\t&lt;link rel=\"stylesheet\" href=\"https:\/\/stackpath.bootstrapcdn.com\/bootstrap\/4.1.3\/css\/bootstrap.min.css\"&gt;\r\n\t\t&lt;script type=\"text\/javascript\"&gt;\r\n\t\t\t\/\/ Declaring enum\r\n\t\t\tconst Direction = {\r\n\t\t\t\tUP: 'UP',\r\n\t\t\t\tDOWN: 'DOWN',\r\n\t\t\t\tLEFT: 'LEFT',\r\n\t\t\t\tRIGHT: 'RIGHT'\r\n\t\t\t};\r\n\r\n\t\t\tfunction sayDirection() {\r\n\t\t\t\tvar direction = document.getElementById(\"dir_idx\").value;\r\n\t\t\t\tswitch (direction) {\r\n\t\t\t\t\tcase Direction.UP:\r\n\t\t\t\t\t\tdocument.getElementById(\"result\").innerHTML = 'We are going up!';\r\n\t\t\t\t\t\tbreak;\r\n\t\t\t\t\tcase Direction.DOWN:\r\n\t\t\t\t\t\tdocument.getElementById(\"result\").innerHTML = 'Heading all the way down. . .';\r\n\t\t\t\t\t\tbreak;\r\n\t\t\t\t\tcase Direction.LEFT:\r\n\t\t\t\t\t\tdocument.getElementById(\"result\").innerHTML = 'Left not right!';\r\n\t\t\t\t\t\tbreak;\r\n\t\t\t\t\tcase Direction.RIGHT:\r\n\t\t\t\t\t\tdocument.getElementById(\"result\").innerHTML = 'To the right.';\r\n\t\t\t\t\t\tbreak;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t&lt;\/script&gt;\r\n\t\t&lt;style type=\"text\/css\"&gt;\r\n\t\t\t#enum-info-result, #code, #result {\r\n\t\t\t\tmargin-left: 16px;\r\n\t\t\t}\r\n\t\t\t.line-hgt {\r\n\t\t\t\tline-height: 2.0;\r\n\t\t\t}\r\n\t\t&lt;\/style&gt;\r\n\t&lt;\/head&gt;\r\n\t&lt;body&gt;\r\n\t\t&lt;div class=\"container\"&gt;\r\n\t\t\t&lt;h2 align=\"center\" class=\"text-danger\"&gt;JavaScript Enums Example&lt;\/h2&gt;\r\n\t\t\t&lt;hr \/&gt;\r\n\t\t\t\r\n\t\t\t&lt;!------ JavaScript Enums Example ------&gt;\r\n\t\t\t&lt;div class=\"form-group\"&gt;\r\n\t\t\t\t&lt;!-- What is Enum? --&gt;\r\n\t\t\t\t&lt;div id=\"enum-info-result\"&gt;\r\n\t\t\t\t\t&lt;h4 class=\"text-primary font-weight-bold\"&gt;Enums&lt;\/h4&gt;\r\n\t\t\t\t\t&lt;ol class=\"text-info line-hgt\"&gt;\r\n\t\t\t\t\t\t&lt;li&gt;In a programming language, an enumerated type (also called &lt;em&gt;enumeration&lt;\/em&gt; or &lt;em&gt;enum&lt;\/em&gt;) is a data-type consisting of a set of named values considered  elements.&lt;\/li&gt;\r\n\t\t\t\t\t\t&lt;li&gt;They behave like &lt;strong&gt;constants&lt;\/strong&gt; in a programming language.&lt;\/li&gt;\r\n\t\t\t\t\t\t&lt;li&gt;E.g. &lt;pre&gt;enum WeekDay = { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY };&lt;\/pre&gt;&lt;\/li&gt;\r\n\t\t\t\t\t&lt;\/ol&gt;\r\n\t\t\t\t&lt;\/div&gt;\r\n\t\t\t\t&lt;!-- Direction selection drop-down --&gt;\r\n\t\t\t\t&lt;div id=\"code\"&gt;\r\n\t\t\t\t\t&lt;h4 class=\"text-sucess\"&gt;Code Snippet&lt;\/h4&gt;&lt;div&gt;\u00a0&lt;\/div&gt;\r\n\t\t\t\t\t&lt;div class=\"col-sm-4\"&gt;\r\n\t\t\t\t\t\t&lt;select id=\"dir_idx\" class=\"form-control\" onchange=\"sayDirection();\"&gt;\r\n\t\t\t\t\t\t\t&lt;option selected=\"selected\" value=\"-1\" disabled&gt;Select direction&lt;\/option&gt;\r\n\t\t\t\t\t\t\t&lt;option value=\"UP\"&gt;Up&lt;\/option&gt;\r\n\t\t\t\t\t\t\t&lt;option value=\"DOWN\"&gt;Down&lt;\/option&gt;\r\n\t\t\t\t\t\t\t&lt;option value=\"LEFT\"&gt;Left&lt;\/option&gt;\r\n\t\t\t\t\t\t\t&lt;option value=\"RIGHT\"&gt;Right&lt;\/option&gt;\r\n\t\t\t\t\t\t&lt;\/select&gt;\r\n\t\t\t\t\t&lt;\/div&gt;\r\n\t\t\t\t&lt;\/div&gt;\r\n\t\t\t&lt;\/div&gt;\r\n\t\t\t&lt;div&gt;\u00a0&lt;\/div&gt;\r\n\t\t\t\r\n\t\t\t&lt;!-- Result --&gt;\r\n\t\t\t&lt;h5 id=\"result\" class=\"text-info\"&gt;&lt;\/h5&gt;\r\n\t\t&lt;\/div&gt;\r\n\t&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<h2>4. Run the Application<\/h2>\n<p>As we are ready for all the changes, let us compile the project and deploy the application on the Tomcat7 server. To deploy the application on Tomat7, right-click on the project and navigate to <code>Run as -&gt; Run on Server<\/code>.<\/p>\n<figure id=\"attachment_23164\" aria-describedby=\"caption-attachment-23164\" style=\"width: 780px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2018\/11\/deploy_img1.jpg\"><img decoding=\"async\" class=\"wp-image-23164 size-full\" src=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2018\/11\/deploy_img1.jpg\" alt=\"JavaScript Enums - Deploy Application on Tomcat\" width=\"780\" height=\"389\" srcset=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2018\/11\/deploy_img1.jpg 780w, https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2018\/11\/deploy_img1-300x150.jpg 300w, https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2018\/11\/deploy_img1-768x383.jpg 768w\" sizes=\"(max-width: 780px) 100vw, 780px\" \/><\/a><figcaption id=\"caption-attachment-23164\" class=\"wp-caption-text\">Fig. 6: How to Deploy Application on Tomcat<\/figcaption><\/figure>\n<p>Tomcat will deploy the application in its web-apps folder and shall start its execution to deploy the project so that we can go ahead and test it in the browser.<\/p>\n<h2>5. Project Demo<\/h2>\n<p>Open your favorite browser and hit the following URL to display the application\u2019s index page as shown in Fig. 7.<\/p>\n<pre class=\"brush:plain; wrap-lines:false;\">http:\/\/localhost:8082\/JavaScriptEnum\/<\/pre>\n<p>Server name (localhost) and port (8082) may vary as per your Tomcat configuration.<\/p>\n<figure id=\"attachment_23165\" aria-describedby=\"caption-attachment-23165\" style=\"width: 818px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2018\/11\/demo-img1-2.jpg\"><img decoding=\"async\" class=\"wp-image-23165 size-full\" src=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2018\/11\/demo-img1-2.jpg\" alt=\"JavaScript Enums - Index page\" width=\"818\" height=\"278\" srcset=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2018\/11\/demo-img1-2.jpg 818w, https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2018\/11\/demo-img1-2-300x102.jpg 300w, https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2018\/11\/demo-img1-2-768x261.jpg 768w\" sizes=\"(max-width: 818px) 100vw, 818px\" \/><\/a><figcaption id=\"caption-attachment-23165\" class=\"wp-caption-text\">Fig. 7: Index page<\/figcaption><\/figure>\n<p>Users can select the values from the dropdown to show the relevant text from the enum constant as shown in Fig. 8.<\/p>\n<figure id=\"attachment_23166\" aria-describedby=\"caption-attachment-23166\" style=\"width: 818px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2018\/11\/demo-img2.jpg\"><img decoding=\"async\" class=\"wp-image-23166 size-full\" src=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2018\/11\/demo-img2.jpg\" alt=\"JavaScript Enums - Enumeration in JavaScript\" width=\"818\" height=\"327\" srcset=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2018\/11\/demo-img2.jpg 818w, https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2018\/11\/demo-img2-300x120.jpg 300w, https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2018\/11\/demo-img2-768x307.jpg 768w\" sizes=\"(max-width: 818px) 100vw, 818px\" \/><\/a><figcaption id=\"caption-attachment-23166\" class=\"wp-caption-text\">Fig. 8: Enumeration in JavaScript<\/figcaption><\/figure>\n<p>That is all for this tutorial and I hope the article served you whatever you were looking for. Happy Learning and do not forget to share!<\/p>\n<h2>6. Conclusion<\/h2>\n<p>In this section, developers learned how to create a simple application with the JavaScript language. Developers can download the sample application as an Eclipse project in the <a href=\"#projectDownload\">Downloads<\/a> section.<\/p>\n<h2><a name=\"projectDownload\"><\/a>7. Download the Eclipse Project<\/h2>\n<p>This was an example of JavaScript Enums for the beginners.<\/p>\n<div class=\"download\"><strong>Download<\/strong><br \/>\nYou can download the full source code of this example here: <a href=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2018\/11\/JavaScriptEnum.zip\" target=\"_blank\" rel=\"noopener\"><strong>JavaScriptEnum<\/strong><\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Greetings readers, in this tutorial, we will show how to use the JavaScript enumerations. 1. Introduction JavaScript is an object-oriented programming language that allows the client-side scripting to interact with a user and deliver the dynamic pages. Most web browsers including Google Chrome, Mozilla Firefox, Safari, Internet Explorer, Microsoft Edge, Opera, etc. support it. The &hellip;<\/p>\n","protected":false},"author":2162,"featured_media":920,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[82],"class_list":["post-23158","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript","tag-javascript"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>JavaScript Enums Example - Web Code Geeks - 2026<\/title>\n<meta name=\"description\" content=\"Interested to learn more about JavaScript? Then check out our detailed example on JavaScript Enums! Download our FREE JS Interview Questions &amp; Answers ebook!\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.webcodegeeks.com\/javascript\/javascript-enums-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JavaScript Enums Example - Web Code Geeks - 2026\" \/>\n<meta property=\"og:description\" content=\"Interested to learn more about JavaScript? Then check out our detailed example on JavaScript Enums! Download our FREE JS Interview Questions &amp; Answers ebook!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.webcodegeeks.com\/javascript\/javascript-enums-example\/\" \/>\n<meta property=\"og:site_name\" content=\"Web Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webcodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2018-11-09T14:15:21+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Yatin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Yatin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/javascript-enums-example\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/javascript-enums-example\/\"},\"author\":{\"name\":\"Yatin\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/6c946b8aea919fb2cd83fb17268e9367\"},\"headline\":\"JavaScript Enums Example\",\"datePublished\":\"2018-11-09T14:15:21+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/javascript-enums-example\/\"},\"wordCount\":839,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/javascript-enums-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg\",\"keywords\":[\"javascript\"],\"articleSection\":[\"JavaScript\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.webcodegeeks.com\/javascript\/javascript-enums-example\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/javascript-enums-example\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/javascript\/javascript-enums-example\/\",\"name\":\"JavaScript Enums Example - Web Code Geeks - 2026\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/javascript-enums-example\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/javascript-enums-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg\",\"datePublished\":\"2018-11-09T14:15:21+00:00\",\"description\":\"Interested to learn more about JavaScript? Then check out our detailed example on JavaScript Enums! Download our FREE JS Interview Questions & Answers ebook!\",\"breadcrumb\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/javascript-enums-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.webcodegeeks.com\/javascript\/javascript-enums-example\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/javascript-enums-example\/#primaryimage\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/javascript-enums-example\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.webcodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript\",\"item\":\"https:\/\/www.webcodegeeks.com\/category\/javascript\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"JavaScript Enums Example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"name\":\"Web Code Geeks\",\"description\":\"Web Developers Resource Center\",\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.webcodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webcodegeeks\",\"https:\/\/x.com\/webcodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/6c946b8aea919fb2cd83fb17268e9367\",\"name\":\"Yatin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/3f011dd665043468ba193f7b07472ebbedfa359cff5e576a91a5901c130ca6f1?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/3f011dd665043468ba193f7b07472ebbedfa359cff5e576a91a5901c130ca6f1?s=96&d=mm&r=g\",\"caption\":\"Yatin\"},\"description\":\"The author is graduated in Electronics &amp; Telecommunication. During his studies, he has been involved with a significant number of projects ranging from programming and software engineering to telecommunications analysis. He works as a technical lead in the information technology sector where he is primarily involved with projects based on Java\/J2EE technologies platform and novel UI technologies.\",\"sameAs\":[\"https:\/\/www.webcodegeeks.com\"],\"url\":\"https:\/\/www.webcodegeeks.com\/author\/yatin-batra\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"JavaScript Enums Example - Web Code Geeks - 2026","description":"Interested to learn more about JavaScript? Then check out our detailed example on JavaScript Enums! Download our FREE JS Interview Questions & Answers ebook!","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.webcodegeeks.com\/javascript\/javascript-enums-example\/","og_locale":"en_US","og_type":"article","og_title":"JavaScript Enums Example - Web Code Geeks - 2026","og_description":"Interested to learn more about JavaScript? Then check out our detailed example on JavaScript Enums! Download our FREE JS Interview Questions & Answers ebook!","og_url":"https:\/\/www.webcodegeeks.com\/javascript\/javascript-enums-example\/","og_site_name":"Web Code Geeks","article_publisher":"https:\/\/www.facebook.com\/webcodegeeks","article_published_time":"2018-11-09T14:15:21+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg","type":"image\/jpeg"}],"author":"Yatin","twitter_card":"summary_large_image","twitter_creator":"@webcodegeeks","twitter_site":"@webcodegeeks","twitter_misc":{"Written by":"Yatin","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.webcodegeeks.com\/javascript\/javascript-enums-example\/#article","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/javascript-enums-example\/"},"author":{"name":"Yatin","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/6c946b8aea919fb2cd83fb17268e9367"},"headline":"JavaScript Enums Example","datePublished":"2018-11-09T14:15:21+00:00","mainEntityOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/javascript-enums-example\/"},"wordCount":839,"commentCount":1,"publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/javascript-enums-example\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg","keywords":["javascript"],"articleSection":["JavaScript"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.webcodegeeks.com\/javascript\/javascript-enums-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.webcodegeeks.com\/javascript\/javascript-enums-example\/","url":"https:\/\/www.webcodegeeks.com\/javascript\/javascript-enums-example\/","name":"JavaScript Enums Example - Web Code Geeks - 2026","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/javascript-enums-example\/#primaryimage"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/javascript-enums-example\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg","datePublished":"2018-11-09T14:15:21+00:00","description":"Interested to learn more about JavaScript? Then check out our detailed example on JavaScript Enums! Download our FREE JS Interview Questions & Answers ebook!","breadcrumb":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/javascript-enums-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.webcodegeeks.com\/javascript\/javascript-enums-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/javascript\/javascript-enums-example\/#primaryimage","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.webcodegeeks.com\/javascript\/javascript-enums-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.webcodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"JavaScript","item":"https:\/\/www.webcodegeeks.com\/category\/javascript\/"},{"@type":"ListItem","position":3,"name":"JavaScript Enums Example"}]},{"@type":"WebSite","@id":"https:\/\/www.webcodegeeks.com\/#website","url":"https:\/\/www.webcodegeeks.com\/","name":"Web Code Geeks","description":"Web Developers Resource Center","publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.webcodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.webcodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.webcodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webcodegeeks","https:\/\/x.com\/webcodegeeks"]},{"@type":"Person","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/6c946b8aea919fb2cd83fb17268e9367","name":"Yatin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/3f011dd665043468ba193f7b07472ebbedfa359cff5e576a91a5901c130ca6f1?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3f011dd665043468ba193f7b07472ebbedfa359cff5e576a91a5901c130ca6f1?s=96&d=mm&r=g","caption":"Yatin"},"description":"The author is graduated in Electronics &amp; Telecommunication. During his studies, he has been involved with a significant number of projects ranging from programming and software engineering to telecommunications analysis. He works as a technical lead in the information technology sector where he is primarily involved with projects based on Java\/J2EE technologies platform and novel UI technologies.","sameAs":["https:\/\/www.webcodegeeks.com"],"url":"https:\/\/www.webcodegeeks.com\/author\/yatin-batra\/"}]}},"_links":{"self":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/23158","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/users\/2162"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/comments?post=23158"}],"version-history":[{"count":0,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/23158\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media\/920"}],"wp:attachment":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media?parent=23158"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/categories?post=23158"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/tags?post=23158"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}