What is the difference between global, globalThis, and this in JavaScript?
January 6, 2025
global
: In Node.js,global
is a special object that refers to the global context, meaning it’s the global object in the Node.js environment. It provides access to global variables and functions, similar to thewindow
object in the browser.globalThis
: This is a standard and universal way to refer to the global object across different JavaScript environments (browser, Node.js, etc.). It is available in both Node.js and browsers, ensuring you can access the global object in any environment in a consistent manner. BeforeglobalThis
, each environment had different global object names (global
in Node.js,window
in the browser).this
: The value ofthis
depends on the context in which it is used. In the global context,this
usually refers to the global object, but within functions or classes,this
behaves differently. For example, inside a function, it refers to the object from which the function is called, or it can beundefined
in strict mode.
No comments yet.