Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Even though this is an old question and answer, it discusses a topic that to this day throws many developers for a loop. I can't count the number of JavaScript developer candidates I've interviewed who couldn't tell me the difference between a function declaration and a function expression <em>and</em> who had no clue what an immediately invoked function expression is.</p> <p>I'd like to mention, though, one very important thing which is that Premasagar's code snippet wouldn't work even if he had given it a name identifier.</p> <pre><code>function someName() { alert(2 + 2); }(); </code></pre> <p>The reason this wouldn't work is that the JavaScript engine interprets this as a function declaration followed by a completely unrelated grouping operator that contains no expression, and grouping operators <em>must</em> contain an expression. According to JavaScript, the above snippet of code is equivalent to the following one.</p> <pre><code>function someName() { alert(2 + 2); } (); </code></pre> <p>Another thing I'd like to point out that may be of some use to some people is that any name identifier you provide for a function expression is pretty much useless in the context of the code except from within the function definition itself.</p> <pre><code>var a = function b() { // do something }; a(); // works b(); // doesn't work var c = function d() { window.setTimeout(d, 1000); // works }; </code></pre> <p>Of course, using name identifiers with your function definitions is always helpful when it comes to debugging code, but that's something else entirely... :-)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload