Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8883939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:56:29+00:00 2026-06-14T20:56:29+00:00

I don’t know am I doing right or wrong, my understanding is not quite

  • 0

I don’t know am I doing right or wrong, my understanding is not quite clear. I am always making many external js files, and put them all in the header. All my external js files begin the same way like this:

$(document).ready(function(){
  //all the functions and variables
});

For example, if I have 3 external js files:

1.

$(document).ready(function(){
  var a = 1000;
  function run(){alert(a)};
  $('#btna').click(function(){run();});
});

2.

$(document).ready(function(){
  var b = 2000;
  function run(){alert(b)};
  $('#btnb').click(function(){run();});
});

3.

I never tried to use variables and functions that are in other JS files. Is it possible to get the a and b variable in the third JS file?

$(document).ready(function(){
  //is it possiblr to do this?
  alert(a+b);
  //or if I use run(), which methods will be called?
  run();
});

I am so confused… How does external JS actually load into html? Or I shouldn’t put document ready in all files? Now I want to load scripts one by one after one is completely loaded, not having a bunch of things load at the same time. Therefore I have to make it clear first.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T20:56:30+00:00Added an answer on June 14, 2026 at 8:56 pm

    Do NOT start putting variables in global scope. While this is sometimes necessary, you are much better off sharing modules between different scopes using something like http://requirejs.org. By using modules and objects, you limit the scope of the variable and reduce the chance of errors due to conflicts between variables defined in different files with the same scope. RequireJS also gives you a way to manage your dependencies, asynchronously load javascript files, and provides a clean way to optimize your scripts if desired.

    From a learning perspective, as others have noted, the difficulties you are observing are due to the scope of the variables being local to the anonymous functions run on document ready. In JavaScript variables defined with var have scope local to the containing block of code. If you don’t define the variable using var then it is scoped globally. Using global scope should be avoided to reduce the chance of accidental reuse of a variable in multiple locations. A good rule of thumb is to limit scope as much as possible, defining it local to where it is used. If a variable must be shared globally, you should probably define it as a property of an object and share the object globally. While the object is shared globally, the object context functions as the scope of the variable (like a namespace) making it less likely that if you will accidentally reuse it somewhere else for a different purpose.

    For example:

    <script type="text/javascript">
         var globalConstants = {
              a = 5,
              b = 10
         };
    </script>
    

    Then you can later refer to globalConstants.a or globalConstants.b – this makes it less likely that you would accidentally repeat the use of a or b in separate scripts without knowing that they are, in fact, the same variables.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Don't know if it's I'm doing it wrong or if there's a bug (I
Don't know why but font is not displaying.Please help. CSS(in css folder): style.css: @font-face
Don't know why people do not practice AJAX implementation for authentication systems. Is it
Don't know what's wrong here, when I run the application it says Specified method
Don't know if this is the right place to ask this, but I will
Don't know if I worded the question right, but basically what I want to
Don't know if I'm over-thinking this or not.. but I'm trying to be able
Don't get me wrong, I want them to get saved. But I always thought
Don't know why it's not working, i think the code is fine - could
Don't quite understand why this copy constructor is not invoked when I build with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.