According to the Node documentation, the top-level scope is not the global-scope, and a variable defined insiede a Node.js module will be local to that module.
However, I noticed that var something
created in the global scope gets listed as a property of global
, which to me seems to be a sort of cross-module object.
Here is the code I ran in GitBash:
$ node
var something = "this is a test"
undefined
something
'this is a test'
console.log(global)
I really don't get how this reconciles with what is stated in the documentation about the top-level scope.
If global
really is a cross-module object, as Ebohlman's answer to this old question seems to suggest, then var something
is not local to the module it gets defined in.
Could someone help me shed some light on this matter? Is var something
really module-scoped? How is global
different from window
in JavaScript? What's its purpose?