Node global vs. window Object

You know how when you declare a variable with var in a browser, it will be added to the window object.

var myVar = 'Hi';
console.log(myVAr === window.myVar); // true

That’s not the case with the global object.

console.log(myVAr === global.myVar); // false

In node, every file is a module. If you want to use a variable outside a module, you have to export it and then import it.

You have the same objects and methods as in the window object, but you also get some extra tools. The most used are probably __dirname and __filename, along with the require() function. For a full list of global objects, you can consult the official documentation.

The following two tabs change content below.

Jay Galaczi

Latest posts by Jay Galaczi (see all)

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *