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

  • Home
  • SEARCH
  • 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 6794075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:10:41+00:00 2026-05-26T18:10:41+00:00

I have the following JavaScript code: (function() { function f(){ alert(1); } return f();

  • 0

I have the following JavaScript code:

(function() { 
    function f(){ alert(1); }
    return f();
    function f(){ alert(2); }
})();

Can you explain why the alert pops up with 2 and not 1?

Thanks,

  • 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-26T18:10:42+00:00Added an answer on May 26, 2026 at 6:10 pm

    This gets into what happens when execution enters a function: Leaving out a lot of detail, all function declarations (the style you’ve used) are processed, and only after that does step-by-step code execution occur. So your return statement has no impact on which function declaration is chosen. And the declaration chosen is always the last one in source code order (this is covered — in wonderfully turgid prose — in Section 10.5 of the specification).

    The result would be fundamentally different if you used function expressions, which are evaluated as part of step-by-step code:

    (function() { 
        var f;
        f = function(){ alert(1); };
        return f();
        f = function(){ alert(2); };
    })();
    

    This code is technically incorrect (because you have code following a return that will always be followed), but it illustrates the difference: You see the alert(1) happen rather than the alert(2), because those expressions are not evaluated until they’re reached.

    You can read more about what happens when execution enters a function (declarations aren’t the only thing that are done prior to the first step-by-step code) in Sections 10.4.3 and 10.5 of the specification.


    And with your new knowledge, a quiz: What happens here? (Note: Never do this.)

    function foo() {
    
        if (true) {
            function bar() {
                alert(1);
            }
        }
        else {
            function bar() {
                alert(2);
            }
        }
    
        bar();
    }
    
    foo();
    

    The answer is: It varies, don’t do that. Some engines will use the first bar, other engines will use the second, and others will call it a syntax error. This is because this actually is an error, and so engines are free to do what they think best. If you look closely at the language grammar, you’ll see that it’s invalid to put function declarations inside branches within their immediately-containing scope. They must be at the top level of that scope. With your new understanding of declarations, the reason should be obvious: They’re not related to the flow of execution within the scope, and so naturally you can’t choose them based on that flow of execution.

    So what happens in the real world? Sadly, usually not an error if you’re in “loose” mode (not strict). Some engines (Chrome’s V8 for example, as of this writing) will ignore the flow control statements and just pick the last function declared (and so you get the counter-intuitive result that the second bar function is used), other engines (Firefox’s SpiderMonkey, IE11’s JScript) effectively rewrite your code on the fly turning those declarations into expressions instead, and so you get the first bar. E.g., it will vary by engine. The good news is that if you try this in strict mode, all three of those (V8, SpiderMonkey, and IE11’s JScript) will fail rather than picking one (V8 and SpiderMonkey with nice clear error messages in the console; JScript with just the surprising “bar is undefined”, but…).

    If you want to do something like the above, but valid and consistent across engines, use expressions:

    function foo() {
        var bar;
    
        if (true) {
            bar = function() {
                alert(1);
            };
        }
        else {
            bar = function() {
                alert(2);
            };
        }
    
        bar();
    }
    
    foo();
    

    There’s a fun exploration of this on kangax’s Named Function Expressions Demystified page.

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

Sidebar

Related Questions

I have the following Javascript code add_num = { f: function(html, num) { alert(this.page);
I have the following JavaScript code: Link In which the function makewindows does not
I have the following javascript code, which loads without error, however the update function
So I have the following JavaScript code: <script type=text/javascript> function clearRadioButtons() { document.getElementById(radiobutton1).checked=; //etc
I have problems with the following bit of javascript/jquery code: this.droppable = function(){ $('.imageWindow
I have the following code: <script type=text/javascript> function SubmitForm() { form1.submit(); } function ShowResponse()
I have the following code: <script type=text/javascript> $(document).ready(function() { $(#Save).click(function() { $.post(url, { data:
I have Designed the Javascript function My.js it contains following code My.js function display(){
I have the following simple javascript code, which handles the Return Key, I don't
I have the following code, which will not work. The javascript gives no errors

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.