How to declare a global variable in Node.js
In this tutorial, we are going to learn about how to declare and use global variables in Node.
In Node.js, each module has its own scope the variables we declared inside one module can’t be accessed inside other modules.
Global variables helps us to access the variable data accross the all modules in our app, so that we can store the common data in one place.
Declaring a global variable
We can declare a global variable in node.js, by using the global
object.
Here is an example, that declares the title
global variable.
global.title = "Cloth store";
Using the global variable
Now, we can access the title
global inside the other modules of our node app like this.
console.log(title); // "Cloth store"