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.
We can declare a global variable in node.js, by using the
Here is an example, that declares the
title global variable.
global.title = "Cloth store";
Now, we can access the
title global inside the other modules of our node app like this.
console.log(title); // "Cloth store"