26

I have created one javascript file in which I have declared different string constants.

now in another javascript file I want to use those String constants from already created javascript file.

Is there any way to do this.

Thanks in advance.

Nitesh
  • 1,477
  • 5
  • 23
  • 34
  • 2
    Are you running it in a browser? If yes, just source the second file after the first. – blgt Oct 30 '14 at 10:39

3 Answers3

16

If you declare your constants in file1 as global variables:

var someConstant = 42;

Then you can just use that variable in your other JS files. Just make sure file1 is loaded before you try to use them.

However, polluting the global scope like this come with it's risks, as those variables are easily changed.

Community
  • 1
  • 1
Cerbrus
  • 70,800
  • 18
  • 132
  • 147
  • 2
    This should not be accepted as answer. We all know global variables are bad, not generic and not neat – canbax Mar 21 '19 at 08:30
  • @canbax: Hence that last line in my answer. – Cerbrus Mar 21 '19 at 08:31
  • 2
    This is not a constant – still_dreaming_1 Feb 25 '20 at 14:08
  • 8
    @still_dreaming_1: At the time this question was asked (this question is almost ___6 years old___), this was a decent answer. Keep in mind that `const` was first [introduced with ES2015](https://www.ecma-international.org/ecma-262/6.0/), in June of 2015. This answer predates the existence of `const`. – Cerbrus Feb 25 '20 at 14:12
7

Multiple ways.

Concatenate

Use a task runner like grunt to define a task that concatenates your files into a single one. This will not only allow convenient definition of constants but also improve performance.

There are other tools to do this, too. See Prepros for windows and codekit for Macs.

Modularize

If you are on client side, use a tool like require.js or browserify to define commonJS / AMD modules and require them as you need.

If you are on server side using node, this works out of the box for commonJS.

Load scripts in correct order, expose them through global objects.

You could load your constant defining script first and do something like this:

var constants = {
    MY_CONST: 'Foo'
}

Your main, whi script could access this variable. If you omit the var keyword, the variable becomes globally available. Note however that this should be avoided.

You could even add a property to the global object (window on the client side).


Personally I like to use commonJS modules when working on projects that allow me to do so. Rob Ashton has an opinion on this that I would like to share.

When I can't use those modules in a convenient way, which would be the case when working with (custom) web-components because of their isolated scopes, I like to add a single script which creates an Object like App. I use this object to expose modules, services & constants which can then be required by any component in a neat way:

App.myService.doSomething()

Wottensprels
  • 3,307
  • 2
  • 29
  • 38
2
  1. Create a file which contains all the constants and variables

Constants.js

const EMAIL = "xyz@gmail.com"
const PHONE = "9911223344"
var NAME = "Default name"
  1. Access the Constants.js file in your HTML file

xyz.html

<script src="Constants.js"></script>
  1. Now you can access the Constants in any of file inside a project
  • I would import like this: `import { EMAIL } from "./Constants";` in the .js file and don't forget, when you attach the script file to the HTML add a `type="module"` like this: `` – Lepy Mar 02 '22 at 10:25