Node.js vs Javascript Closure

I'm working my way through the Eloquent JavaScript Book and in it there is the following code:

function createFunction(){
  var local = 100;
  return function(){return local;};
}

When I run this via the node console (run node from command prompt) by calling createFunction(), I get [Function] as a returned value. However, according to the book I should get 100.

So my two questions: Why is this? and Second, is running these little examples in the node console a bad idea for testing JS code?

You need to call the response of createFunction().

createFunction()();

The first invocation (()) calls createFunction() and returns the inner function, which the second invocation executes and returns the local variable which was closed over.

Running small examples in a node console (or any other) is fine, so long as you know the environment, e.g. a browser's console is generally eval()'d, which can create side effects, such as how delete can apparently delete variables, not just object properties.

You get 100 by invoking the return value of createFunction, which is itself a function.

createFunction()();

...or perhaps more clearly...

var new_func = createFunction();

new_func();

function createFunction(){
  var local = 100;

 //  v---v-----------------------v return a function from createFunction
  return function(){return local;};
}

  //  v------- the returned function is assigned to the new_func variable
var new_func = createFunction();

 //   v------- the returned function is invoked
new_func();

For those that have a similar problem, I completely missed the double () so the call looks like createFunction()().