What is local, script and global scope in Node.js ?
Node.js is an open-source and cross-platform runtime environment for executing JavaScript code outside a browser. You need to remember that NodeJS is not a framework and it’s not a programming language. Most people are confused and understand it’s a framework or a programming language. We often use Node.js for building back-end services like APIs like Web App or Mobile App. It’s used in production by large companies such as Paypal, Uber, Netflix, Walmart, and so on.
Scope of a Variable: The word scope means the extent to which we can use something. In programming variable scope means the extent to which we can use a variable within a program.
In JavaScript there are mainly 3 types of scopes:
- Local scope
- Global scope
- Script scope
Local Scope:
- The Node.js application starts with the local scope enabled. Local variables are defined by the functions. Variables having a local scope restricts their usage to the function in which they are defined.
- It’s also known as Default Scope.
Example 1: An example of defining variables under local scope-
Javascript
var d = 'Tom' ; function zeta() { var d; if (d === undefined) { d = 'Jerry' ; } console.log(d); } zeta(); |
Output:
Jerry
Explanation: The zeta function is used to declare a new variable d which possesses a local scope. In JavaScript, the variables are initiated at the top of the current scope no matter where it is included in the code while we use var to declare them.
Example 2: Here is another example of declaring variables under local scope –
Javascript
var locales = { europe: function () { // The Europe continent's local scope var myFriend = "Monique" ; // France country's local scope var france = function () { // The Paris city's local scope var paris = function () { // output: Monique console.log(myFriend); }; paris(); }; france(); } }; locales.europe(); |
Output:
Monique
Explanation: The myFriend variable was declared in the outer scope of the France function and is thus accessible from the Paris function in this case. MyFriend is not declared if the myFriend variable and the console statement are switched.
Global Scope:
- When you’re in the global scope var something of a browser will define a global variable because the global scope is the highest scope in browsers where it is not the same case in Node. Inside a Node module, a variable will be limited to that module only; and global scope will not be the top scope anymore.
Example 1: An example of declaring variables under Global scope-
Javascript
// Global Scope const name = "Monique" ; function sayHi() { console.log(`Hi ${name}`); } sayHi(); // Hi Monique |
Output:
Hi Monique
Explanation: We can see from this simple example given above that the variable name is global. It is available throughout the entire application and is defined in the global scope.
Example 2: Here is another example illustrating global scope –
Javascript
//Global Variable in JavaScript var scope = "Global" ; //Global Variable console.log(scope); //Global function access() { console.log(scope); // Accessing a global variable } access(); //Global |
Output:
Global Global
Explanation: Given above is a simple program in JavaScript in which we have declared the variable Global using the keyword var under the global scope. In Global scope variables we can use the three keywords: let, const, and var.
Script:
- The Script scope is actually a place to keep the variables that are shared and strictly not exposed to the Global scope.
- Each child in a script or script module shares the Script scope.
- Essentially, they are a series of terminal commands or terminal commands themselves that assist in automating repetitive chores. NPM scripts as a series of terminal commands can be summed up. Scripts for a project are kept in the package’s section.
Javascript
let GLOBAL_DATA = { value: 1 }; console.log(GLOBAL_DATA); |
Output:
{ value: 1 }
Thanks for reading. Comment below for any improvement or suggestion.
Contact Us