Advertisement
Shaun_B

JavaScript constants

Jun 30th, 2014
619
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script type="text/javascript">
  2.     /**
  3.      * This is one way to do constants in JS
  4.      * Constants are usually UPPERCASE but I put a fail-safe in to accept UPPER, lower and MixED cASe
  5.      * Also did a parseFloat on the return, which may not strictly be necessary - MMXIV Donkeysoft
  6.      */
  7.     function constants(type) {
  8.         _c = [];
  9.     _c["ft"] = 0.3048;
  10.     _c["pi"] = 3.1415;
  11.         return parseFloat( _c[ type.toLowerCase() ] )
  12.     }
  13.     console.log( "There are exactly " + constants("FT") + "metres to the foot" );
  14.     console.log( "The value of PI is exactly " + constants("PI") );
  15. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement