However, when JavaScript code executes at the global level (outside of any function or block) in a browser environment, this refers to the global window object.
Function context and this#
Five primary function contexts in JavaScript can affect the value of this.
Regular function context#
When a regular function is called in the global scope, this refers to the global object (window in browsers, global in Node.js).
Note: The global object depends on the JavaScript execution environment. In a web browser, it’s the window object, while in a Node.js environment, it’s the global object.
In the following code, we define a regular function named myFunction() and invoke it within the global scope. Execute the code to verify that this points to the global object. This behavior is expected because we’ve configured the following widget to run within the Node.js environment: