Forum Discussion

pvidal's avatar
pvidal
Contributor
15 years ago

How to create user defined constants in JScript?

Hello,



I wanted to know how to create user defined constants in JScript. The help in the Code Metrics sugests to use constants for magic numbers, etc. Are these "constants" just variables defined at the beginning of the script? Or there is some king of special "const" keyword?



Thank you,

Patricio.

5 Replies

  • Hello Alex,



    Yes, I found those links before. The only form that does not gives a syntax error in TestComplete is "var <name> = <value>;". All the rest gives you a syntaxt error while processing "const":



        const daysInWeek  = 7;

        const favoriteDay  = "Friday";

        const daysInWeek : int = 7;



    Regards,

    Patricio.

  • Hi Patricio,





    Sorry for the confusion. The articles I gave you are regarding JScript .NET. The scripting language used in TestComplete is not a .NET version. In regular JScript, constants are not supported, so the "const" statement is not available.





    I suggest that you use variables instead. Please see the "About Variables" and the "Code Metrics" articles for the information.
  • To minimize your global namespace, I suggest you create constants under an object literal:



    var Constants = {

      A : 1,

      B : 2,

      C : 3

    };