title: Javascript Enum To Corresponding String Value
body: <p>So I have this in the javascript for my page:</p>
  
  <pre><code>var TEST_ERROR  = {
          'SUCCESS'   :   0,
          'FAIL'      :   -1,
          'ID_ERROR'  :   -2
        };
  </code></pre>
  
  <p>And perform tests on functions in the page like so:</p>
  
  <pre><code>function test()
  {
      // Get the paragraph from the DOM
      var testResultParagraph = document.getElementById('testResult');
  
      // Check the paragraph is valid
      if(!testResultBox)
      {
          // Update the web page
          testResultParagraph.value = TEST_ERROR.ID_ERROR;
          return TEST_ERROR.ID_ERROR;
      }
  
      // Something to store the results
      var testResult = TEST_ERROR.SUCCESS;
  
      // Test the calculation
      testResult = testCalculate()
  
      // Update the web page
      testResultParagraph.value = testResult;
  
      // The test succeeded
      return TEST_ERROR.SUCCESS;
  }
  </code></pre>
  
  <p>The result of <code>testCalculate()</code> and the value of the paragraph will be either 0, -1, -2 depending on the outcome.</p>
  
  <p>Now I want to map this to a string so that the paragraph shows 'Success', 'Fail' or 'ID Error'</p>
  
  <p>I could do this a few ways I have figured:</p>
  
  <pre><code>var TEST_ERROR  = {
          'SUCCESS'   :   {VALUE : 0 , STRING: 'Success' },
          'FAIL'      :   {VALUE : -1, STRING: 'Fail'    },
          'ID_ERROR'  :   {VALUE : -2, STRING: 'Id Error'},
        };
  </code></pre>
  
  <p>would require a modification to the enum dot accessors, or</p>
  
  <pre><code>var TEST_ERROR  = {
          'SUCCESS'   :   0,
          'FAIL'      :   1,
          'ID_ERROR'  :   2
        };
  
  var TEST_STRING = [
          'Success',
          'Fail',
          'ID Error'
        ];
  </code></pre>
  
  <p>Which would require changes to the logic (<code>result &gt; TEST_ERROR.SUCCESS</code> seems wierd tho!)</p>
  
  <p><strong>My question is how would you go about mapping an enumerator value to a string value in Javascript?</strong>  I'm thinking the second way is the most sensible, but would like the enumerator to be positive for successes and negative for fails.  I also like the idea of the first containing the strings and values in the object structure.</p>
  
  <p>Any ideas?</p>
  
  <p>Thanks!</p>
  
  <p>Matt</p>
  
  <p><i>PS.  I'm going to be doing the testing in a Web Worker, so that the page doesn't hang and the results will be put into a table, not a paragraph like above.</p>
  
  <p>PPS. I'm pretty new to Javascript programming, but do a lot in ASM, C, C++, C#.</i></p>