Node.js variable declaration and scope

It doesn’t work in Node when using var because testContext is a local of the current module. You should reference it directly: console.log(testContext);.

When you don’t type var, what happens is that testContext is now a global var in the entire Node process.

In Chrome (or any other browser – well, I’m unsure about oldIE…), it doesn’t matter if you use var or not in your example, testContext will go to the global context, which is window.

By the way, the “global context” is the default this of function calls in JS.

Leave a Comment