How to create a const file in javascript?

Is there a way to create a constant file in JavaScript that I can reference and then use? I am looking for something like this:
Constants.js:

var Phones =
{
Nokia: 1,
Samsung: 2
}

Then, in another JavaScript file, JS2.js refers to these values: JS2.js:
notification (Phones.Nokia);

And then in the aspx file that uses them, refer to both of them, for example:
<asp:ScriptReference Path="../js/JS2.js" />
<asp:ScriptReference Path="../js/Constants.js" />

Is such an architecture possible? What types of data can we use? I just illustrated the enumerations, because this is what I am using right now, but they must be declared in the same file as they are.

+6
javascript enums constants
source share
2 answers

It is very possible (just like you use them). This will not be an enumeration, though, just an object with several number fields.

You should use the script link for constants as the topmost (logically).

Common types are:

  • undefined (variable)
  • null (variable)
  • Line
  • Boolean
  • number
  • Line
  • An object
+2
source share

you are doing it right.

make sure the constants are declared first.

JavaScript parses all scripts and script file links in order from top to bottom.

+2
source share

All Articles