With regard to global variables, why is the output under node and browser different? Is it because of strict mode? Ask for a detailed explanation

node version 10.13.0

var name = "window";
function foo() {
    var name = "inner";
    console.log(this.name);
}
foo();  // undefined

chrome 69 strict mode and non-strict mode

is the difference in strict mode? Is the strict mode of
node enabled automatically?
Thank you guys

Jul.12,2022

the difference is in the scope , node does not enable strict mode by default. In
browser , variables declared under global scope automatically become global variables (an attribute under window). In
node , each module (file) has its own module scope , in which you declare a variable that will not become a global variable of node, but a variable under the scope of this module.


    The this of
  1. in node is actually a {}, which you can print directly in node, so the this.name in node is naturally undefined.
  2. for browsers, in strict mode, if the function is called without specifying an environment object, the this value will not be converted to window, unless the function is explicitly added to an object or apply () or call () is called, otherwise the this value will be undefined.
Menu