1. Running this script in Browser environment forces that top-level var to become a property of the global window object. Then per MDN:
Since the following code is not in strict mode, and because the value of this is not set by the call, this will default to the global object , which is window in a browser.
So calling console.log(this.name); inside that function means that this would be a link to the global window object, which would have name property. And the value of window.name is "Global".
2. NodeJS has no window object, but it has global object instead. Running script in NodeJS environment breaks on two situations:
(a) running as module (e.g. via node test.js) and (b) running not as module (e.g. directly in node console).
The second case (b) works the same as for Browser: global would receive name property and this would be a link to global,
so the value of this.name would be equal to global.name and would be "Global".
But first case (a) is different, per NodeJS doc:
Object The global namespace object.
In browsers, the top-level scope is the global scope. This means that within the browser var something will define a new global variable.
In Node.js this is different. The top-level scope is not the global scope; var something inside a Node.js module will be local to that module.
So that top-level var becomes just a local variable in the Node environment, and since global has no name property,
calling console.log(this.name) shows undefined.