Do you know you can earn heaps of $$ by answering questions on kodlogs, for more info contact us through feedback link at footer? We are Australian based web site and pay you for answering questions

  • Register
Welcome to Kodlogs Q&A, where you can ask programming questions and receive answers from other members of the community. For programmers, by programmers Kodlogs is an open community for anyone that does coding. We help you get answers to your toughest coding questions, share knowledge with your coworkers in private, and find your next IT dream job.
0 votes
3 views
why is it, in general, a good idea to leave the global scope of a website as-is and never touch it?
by (2.7k 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";

}

GloblaScopedtobeAvoided();

console.log(varG ); 

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

ContextObject

DependencyInjection

Hidden Globals

Stateful Procedures

SingletonPattern

Database or TupleSpace or DataDistributionService

Some useful Links to read on:

by (1.4k points)  
...