Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use javascript constant in javascript file from another javascript file

Tags:

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.

like image 684
Nitesh Avatar asked Oct 30 '14 10:10

Nitesh


People also ask

How to include a JavaScript file in another JavaScript file?

Learn How to Include a JavaScript File in Another JavaScript File. We can include a JavaScript file in another JavaScript file using the native ES6 module system. This allows us to share code between different JavaScript files and achieve modularity in the code.

How to access variables from another file in JavaScript?

In JavaScript, variables can be accessed from another file using the <script> tags or the import or export statement. The script tag is mainly used when we want to access variable of a JavaScript file in an HTML file. This works well for client-side scripting as well as for server-side scripting.

How to call a JavaScript file from another function?

There are two popular way to call a JavaScript file from another function those are listed below: Ajax Techniques; Concatenate files. Ajax Techniques Example: External JavaScript file named as “main.js”

How do I access the contents of a JavaScript file?

Since the JavaScript file is imported, the contents are accessible within the HTML file. We create a button which when clicked triggers the JavaScript function. The Student object properties are accessed through the f () function and all the Student object properties are concatenated to a string variable.


1 Answers

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.

like image 131
Cerbrus Avatar answered Sep 28 '22 03:09

Cerbrus