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 6905537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:11:44+00:00 2026-05-27T08:11:44+00:00

If I have two separate scripts in an HTML page with JavaScript are the

  • 0

If I have two separate scripts in an HTML page with JavaScript are the variables shared between the entire page? Or only within their own declarations?

Example:

<script> var title = "Hello World!"; </script>
// random HTML/PHP
<script> document.write(title); </script>

Will that write “Hello World!”?
This seems like bad coding convention how else could I achieve something like this with proper form.

  • 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-05-27T08:11:44+00:00Added an answer on May 27, 2026 at 8:11 am

    Variable title in your example is declared as a global variable, therefore it will be available to any and all scripts loaded into the same page. Whats more, if there is already a global variable named title on the same page, its value will be overwritten when you assign it the value “Hello World!”

    The usual practice to avoid this sort of problem is to declare exactly one global variable, then put all of your other variables inside it. For example:

    var bobbyS_vars = {
        title: "Hello World!";
    };
    

    Assign that lone global variable a name that no one else is likely to choose, such as your name or employer’s name or best-of-all, a domain name that belongs you or your employer.

    Another, more common way to handle this problem is to take advantage of of the way that JavaScript handles variable scope within functions. For example, create an anonymous function, declare all of your code inside that function, then call the function at the end of the declaration by putting () at the end of the declaration. For example:

    (function() {
        var title = "Hello World!";
    
        document.write(title);
    })();
    
    // title is not in scope here, so it is undefined,
    // unless it were declared elsewhere.
    

    If you want to share some variables, but not others, have your anonymous function use a combination of approaches:

    var bobbyS_vars = {
        title: "Hello World!";
    };
    
    (function() {
        var employeeId = "E 298";
        var count = 7;
    
        document.write("<p>" + bobbyS_vars.title + "</p>");
        document.write("<p>" + employeeId + "</p>");
    })();
    
    // At this point, bobbyS_vars.title is in scope and still has the 
    // value "Hello World!". Variables employeeId and count are not
    // in scope and effectively private to the code above.
    

    One final note. All of the functions that your code declares are also effectively global variables. So, if you create a function named printTitle, it is 1) available to all other code on the page and 2) could overwrite or be overwritten by another function on the same page also named printTitle. You can protect and/or expose your functions the same way you would any other variable:

    var bobbyS_vars = { };
    
    (function() {
        // Private functions
        var function = addOne(i) {
            return i + 1;
        };
    
        // Public vars
        bobbyS_vars.title: "Hello World!";
    
        // Public functions
        bobbyS_vars.printTitle = function() {
            document.write("<p>" + bobbyS_vars.title + "</p>");
            document.write("<p>" + addOne(41) + "</p>");
        };
    })();
    
    // At this point, function addOne is not directly accessible,
    // but printTitle is.
    bobbyS_vars.printTitle();
    

    Note that although function addOne is effectively a private function within the closure, it is still accessible indirectly, via the printTitle function because addOne and printTitle are both within the same scope.

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

Sidebar

Related Questions

I have two separate python scripts. One is for getting user location information (which
We have two separate web-apps, say 'retailUI' and 'bulkUI'. These two are basically two
I have two divs and two separate links that triggers slideDown and slideUp for
We have two databases, in two separate locations. One of the databases resides in
Say I have two separate classes, A and B. I also have Repository class
I have two essentially separate applications for configuring two pieces of hardware sold by
I have two physically-separate MySQL databases on which I have to run a single
I have basically two separate sites, a SharePoint collaboration site, and an ASP.Net application
I have two versions of a product and am using separate Hg repositories for
I have an odd need to open two separate instances of excel and having

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.