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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:48:20+00:00 2026-06-05T00:48:20+00:00

If I do this in my <head> tag: <script type=text/javascript src=foo.js></script> And inside foo.js

  • 0

If I do this in my <head> tag:

<script type="text/javascript" src="foo.js"></script>

And inside foo.js I do this:

var foo = new Foo();
function Foo()
{
   //code here
}

Would this code reliably instantiate the variable foo even though its included above the function definition, or should I move it instead to the bottom of the file, like this:

function Foo()
{
   //code here
}
var foo = new Foo();
  • 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-05T00:48:21+00:00Added an answer on June 5, 2026 at 12:48 am

    Your example will work in any browser that follow the ECMAScript standard (all do at least with regards to this question).

    See sections 10.3-10.5 of the specification.

    First the local scope is set up and just thereafter the function body is actually run.

    Read 10.5 (the section is really not very long) to understand why @meder’s answer is right.

    If you don’t want to read the specs yourself:

    10.5 tells to declare variables in that order (overwriting if a some name occurs twice):

    Inheriting from outer scope = setting will affect the outer scope:

    • Variables of the surrounding scope.
    • The own function name.

    Local scope = setting won’t affect the outer scope:

    • The parameters (left to right).
    • The arguments object.
    • Declare all the inner variables (not overwriting the current value if any, undefined if none, yet)

    All in all:

    Returns the function x itself:

    function x() {
        return x;
    }
    

    Returns the parameter x:

    function x(x) {
        return x;
    }
    

    Returns the inner function x:

    function x(x) {
        return x; // the return does no harm, x is already set
        function x() {} // before the actual body is evaluated
    }
    

    Also returns the inner function x:

    function x(x) {
        var x; // in this case a no-op 
        return x;
        function x() {}
    }
    

    Returns 42:

    function x(x) {
        var x = 42; // overwrite x in local scope
        return x;
        function x() {}
    }
    

    Returns the second argument:

    function x(x,x) { // assign left to right, last one "wins"
        return x; // arguments[0] would still name the first argument
    }
    

    Returns 2 when x is called the second time, as x is set to the inner function:

    function x() {
        x = function() { return 2; } // set x in outer scope
        return 1;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Below is the code for my YWA wrapper var astr_ywascript = (document.createElement(script).type = text/javascript).src
Below is my code <script type=text/javascript> var Tag1, Tag2, Tag3, Tag4; var Data1,Data2,Data3,Data4; function
I have a PHP code: if($billing_total>$limit_to_send){ echo '<script type=text/javascript> window.onload = function() { alert(Sorry,
<!DOCTYPE html> <html> <head> <meta http-equiv=Content-Type content=text/html; charset=UTF-8 /> <script src=https://ajax.googleapis.com/ajax/libs/jquery/1.6.3/jquery.min.js></script> <script type=text/javascript> $(
I got this error after validation: document type does not allow element script here;
I have this two lines of html code... <div id=slider1 data-param1=XXX data-param2=XXX></div> <script src=script.js
I want to append a script tag which executes one line of JavaScript to
My website dynamically embeds an external Javascript file into the head tag. The external
I'm attempting to select all <script type=text/html> tags in a page. I use <script>
HTML: <html> <head> <meta http-equiv=Content-Type content=text/html; charset=UTF-8> <title></title> <link type=text/css href=galerina.css rel=stylesheet/> </head> <body>

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.