How to pass variable from one Javascript to another Javascript file [duplicate]


This question already has an answer here:

<ul><li> <a href="/questions/2932782/global-variables-in-javascript-across-multiple-files" dir="ltr" rel="nofollow">Global variables in Javascript across multiple files</a> <span class="question-originals-answer-count"> 7 answers </span> </li> </ul>

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.


see about local and global variables for more info. <a href="http://www.w3schools.com/js/js_scope.asp" rel="nofollow">http://www.w3schools.com/js/js_scope.asp</a>.

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.


A variable in global scope can be access from all javascript file.<br /> 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>


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.


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):

<ul><li>Create a namespace in a third file, MYAPP = {};</li> <li>Use namespace in second file: MYAPP.x = "sachin;"</li> <li>Access variable from same namespace in third file: MYAPP.x</li> </ul>


  • PHP $_POST doesn't display data - Example code shown
  • why the html5 geolocation fails to get results?
  • When do I have to close mysqli (Database) connection?
  • How to implement xsl
  • C# List of Panels
  • JSON encode and decode on PHP
  • Building Qt project for C++11 standard
  • Defined variables not working in javascript files when I use getScript
  • How to return DataSet (xsd) in WCF
  • How can I display the parent menu item's description using Wordpress walkers?
  • Custom validator control occupying space even though display set to dynamic
  • How to rebase a series of branches?
  • DomPDF {PAGE_NUM} not on first page
  • Javascript simulate pressing enter in input box
  • req.body is undefined - nodejs
  • Azure Cloud Service Web Role web pages do not load
  • Cross-Platform Protobuf Serialization
  • Modifying destination and filename of gulp-svg-sprite
  • Importing jscolor library in angular 2
  • Websockets service method fails during R startup
  • Alternatives to the OPTIONAL fallback SPARQL pattern?
  • Do I've to free mysql result after storing it?
  • How to get next/previous record number?
  • Apache 2.4 - remove | delete | uninstall
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • bootstrap to use multiple ng-app
  • How to get icons for entities from eclipse?
  • A cron job substitute?
  • json Serialization in asp
  • Proper way to use connect-multiparty with express.js?
  • Trying to get generic when generic is not available
  • embed rChart in Markdown
  • Turn off referential integrity in Derby? is it possible?
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • JaxB to read class hierarchy
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal