• Register
Welcome to Kodlogs, programming questions and answer website.
0 votes
why is it, in general, a good idea to leave the global scope of a website as-is and never touch it?
by (5.3k points)  
retagged by

1 Answer

0 votes

Like for other languages its same for JS, what is it?

Global variables is not worth as "Good thing". In JS there are more bad consequences.

Resons for why JS global variables are not good and why the global scope should be avoided:

1. Not local: 

They can be easily modified by any code of program they are not local.

2. Limited Access: 

Control and Constraint Checking:

A global  variable can be get or set by any piece of the program, and any rules with respect to its utilization can be effectively broken or overlooked.

3. Coupling Issue: 

Many  global variables means tight couplings.

4.Issues of Concurrency: global variables are not thread safe as multiple threads could access this variable at same time.

5. Pollution of Namesapace

6. Issues of Memory allocation

Some examples of global variable:

function GloblaScopedtobeAvoided() {

    varG = "Test Global Variable";



console.log(varG ); 

Someof alternatives to Global Variables which are worth exploring are following topics:



Hidden Globals

Stateful Procedures


Database or TupleSpace or DataDistributionService

Some useful Links to read on:

by (1.6k points)