How to minimize the use of global variables in JavaScript?


A global variable has global scope which means it can be defined anywhere in your JavaScript code. To minimize the usage of global variables, use consistent variables around different parts of your project.

var myValue = {
   common: function(){
      //Add code
   },
   val1: {
      myVar1: "Demo String One",
      func1: function(){
         //Add some code
      },
   }
   val2: {
      myVar1: "Demo String Two",
      func1: function(){
         //Add another code
      },
   }
   val3: {
      myVar1: "Demo String Three",
      func1: function(){
         //Add another code here
      }
   }
};


Updated on: 12-Sep-2019

188 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements