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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:28:11+00:00 2026-05-17T06:28:11+00:00

Why does the first one of these examples not work, but all the other

  • 0

Why does the first one of these examples not work, but all the other ones do?

// 1 - does not work
(function() {
setTimeout(someFunction1, 10);
var someFunction1 = function() { alert('here1'); };
})();

// 2
(function() {
setTimeout(someFunction2, 10);
function someFunction2() { alert('here2'); }
})();

// 3
(function() {
setTimeout(function() { someFunction3(); }, 10);
var someFunction3 = function() { alert('here3'); };
})();

// 4
(function() {
setTimeout(function() { someFunction4(); }, 10);
function someFunction4() { alert('here4'); }
})();
  • 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-17T06:28:11+00:00Added an answer on May 17, 2026 at 6:28 am

    This is neither a scope problem nor is it a closure problem. The problem is in understanding between declarations and expressions.

    JavaScript code, since even Netscape’s first version of JavaScript and Microsoft’s first copy of it, is processed in two phases:

    Phase 1: compilation – in this phase the code is compiled into a syntax tree (and bytecode or binary depending on the engine).

    Phase 2: execution – the parsed code is then interpreted.

    The syntax for function declaration is:

    function name (arguments) {code}
    

    Arguments are of course optional (code is optional as well but what’s the point of that?).

    But JavaScript also allows you to create functions using expressions. The syntax for function expressions are similar to function declarations except that they are written in expression context. And expressions are:

    1. Anything to the right of an = sign (or : on object literals).
    2. Anything in parentheses ().
    3. Parameters to functions (this is actually already covered by 2).

    Expressions unlike declarations are processed in the execution phase rather than the compilation phase. And because of this the order of expressions matter.

    So, to clarify:


    // 1
    (function() {
    setTimeout(someFunction, 10);
    var someFunction = function() { alert('here1'); };
    })();
    

    Phase 1: compilation. The compiler sees that the variable someFunction is defined so it creates it. By default all variables created have the value of undefined. Note that the compiler cannot assign values yet at this point because the values may need the interpreter to execute some code to return a value to assign. And at this stage we are not yet executing code.

    Phase 2: execution. The interpreter sees you want to pass the variable someFunction to setTimeout. And so it does. Unfortunately the current value of someFunction is undefined.


    // 2
    (function() {
    setTimeout(someFunction, 10);
    function someFunction() { alert('here2'); }
    })();
    

    Phase 1: compilation. The compiler sees you are declaring a function with the name someFunction and so it creates it.

    Phase 2: The interpreter sees you want to pass someFunction to the setTimeout. And so it does. The current value of someFunction is its compiled function declaration.


    // 3
    (function() {
    setTimeout(function() { someFunction(); }, 10);
    var someFunction = function() { alert('here3'); };
    })();
    

    Phase 1: compilation. The compiler sees you have declared a variable someFunction and creates it. As before, its value is undefined.

    Phase 2: execution. The interpreter passes an anonymous function to setTimeout to be executed later. In this function it sees you’re using the variable someFunction so it creates a closure to the variable. At this point the value of someFunction is still undefined. Then it sees you assigning a function to someFunction. At this point the value of someFunction is no longer undefined. 1/100th of a second later the setTimeout triggers and the someFunction is called. Since its value is no longer undefined it works.


    Case 4 is really another version of case 2 with a bit of case 3 thrown in. At the point someFunction is passed to setTimeout it already exists due to it being declared.


    Additional clarification:

    You may wonder why setTimeout(someFunction, 10) doesn’t create a closure between the local copy of someFunction and the one passed to setTimeout. The answer to that is that function arguments in JavaScript are always, always passed by value if they are numbers or strings or by reference for everything else. So setTimeout does not actually get the variable someFunction passed to it (which would have meant a closure being created) but rather only gets the object that someFunction refers to (which in this case is a function). This is the most widely used mechanism in JavaScript for breaking closures (for example in loops) before the invention of the let keyword.

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

Sidebar

Related Questions

First, what does one call the ghost caption that appears in a text edit
I am trying to write my first real python function that does something real.
What does InitializeComponent() do, and how does it work in WPF? In general first,
This is actually a two part question. First,does the HttpContext.Current correspond to the current
Does anyone know of a really simple way of capitalizing just the first letter
Why does the compiler say a constant value is required for the first case...the
Does anybody know how to get thumbnail (still image) from 3gb video file? First
I am calling a csh script that the first thing it does is starts
First post, so here goes. I'm writing a script that does intelligent search and
Short: how does modelbinding pass objects from view to controller? Long: First, based on

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.