Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>addTwo</code> and <code>addFive</code> are variables -- but they're function variables. Look at <code>typeof(addTwo)</code> -- it's a function. It's like if you did this:</p> <pre><code>var addTwo = function(x) { return x + 2; }; </code></pre> <p>It's the same as this:</p> <pre><code>function addTwo(x) { return x + 2; } </code></pre> <p>(Edit: As Šime pointed out, they aren't exactly the same. See <a href="http://www.dustindiaz.com/javascript-function-declaration-ambiguity/" rel="noreferrer">here</a> for an explanation of the difference between the two.)</p> <p>The example will hopefully make sense once you understand that. You can even do weird things like this, declaring an anonymous function and invoking it right away:</p> <pre><code>var seven = function(x) { return x + 2; }(5); </code></pre> <p><strike>Which is literally, at the physical machine code level, the exact same as:</strike> Which is equivalent to for all purposes relevant to this question:</p> <pre><code>function addTwo(x) { return x + 2; } var seven = addTwo(5); </code></pre> <hr> <p>Edit:</p> <p>Perhaps a less confusing "prequel" to this is the following:</p> <pre><code>function makeTheAddTwoFunction() { return function(x) { return x + 2; } } var addTwo = makeTheAddTwoFunction(); </code></pre> <p>This is silly, but serves to illustrate functions that make functions. Of course, that sort of function usually will accept arguments so that it can make different functions each time, but there you go.</p>
 

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