13

I tried the following, trying to pass a variable from one JavaScript file to another JavaScript variable. My first JavaScript file:

var x = "sachin";

My other JavaScript file can't access that x variable value. How do I solve this? I can access that x variable and same value in another file.

Andrew Li
  • 55,805
  • 14
  • 125
  • 143
Aravinth E
  • 449
  • 2
  • 6
  • 29
  • 1
    I know its vague, but try `window.x = "sachin"`. – 31piy Dec 21 '16 at 05:39
  • use with global variable and order the js file one by one `var x = "sachin"` is first js file .Then add next js file in below of the file .order is important .` ` – prasanth Dec 21 '16 at 05:41
  • make sure the file with the variable is before the other file – madalinivascu Dec 21 '16 at 05:41
  • *"I **can** access that x variable and same value in another file."* - Are you saying that `x` in file 1 can't be accessed from file 2 but can be from file 3? In which order do you include the files? – nnnnnn Dec 21 '16 at 05:44
  • 2
    You mean two scripts on the same page or different pages? – epascarello Dec 21 '16 at 05:47
  • This would be a good time to take a few minutes to read through [ask]. Question is too vague without proper details and relevant code – charlietfl Dec 21 '16 at 05:51

4 Answers4

11

see about local and global variables for more info. http://www.w3schools.com/js/js_scope.asp.

Make sure your var X is not inside a function and that your file is load in the correct order.

<script src="file1.js"><script> //declare var x=1 here
<script src="file2.js"><script> // you can access x from here.
chungtinhlakho
  • 870
  • 10
  • 21
  • Basically my project is a simple chat app,which has credentials like API_KEY,like that,does this method works here? – Aashiq Jun 18 '20 at 13:41
11

A variable in global scope can be access from all javascript file.
Your first js file

  //first.js file
    var globalVariable={
       x: 'sachin'
    };

And your second js file

    //second.js file
    alert(globalVariable.x);

And in html page add-

<script type="text/javascript" src="first.js"></script> 
<script type="text/javascript" src="second.js"></script> 
Ashvin
  • 211
  • 2
  • 11
  • 1
    Can you do it without two `script` elements? Basically import `second.js` inside `first.js` –  May 03 '20 at 15:22
1

I'm going to assume that you're running JavaScript in the browser. The order in which you include these files matters. If your script tags are in the wrong order, like...

<script src="file2.js"></script>
<script src="file1.js"></script>

If xis defined in file1, you can't use it in file2. file2 loads and runs first.

bigblind
  • 12,539
  • 14
  • 68
  • 123
0

Two of the simplest ways would be to make the variable global or add it to a particular namespace that both files share.

To make it global (not ideal):

window.x = "sachin";

To add to a common namespace (keep in mind this namespace is global too):

  • Create a namespace in a third file, MYAPP = {};
  • Use namespace in second file: MYAPP.x = "sachin;"
  • Access variable from same namespace in third file: MYAPP.x
Tim Gupta
  • 94
  • 3