15

JavaScript: The Good Parts defines these kinds of declarations as bad:

foo = value;

The book says "JavaScript’s policy of making forgotten variables global creates bugs that can be very difficult to find."

What are some of the problems of these implied global variables other than the usual dangers of typical global variables?

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
unj2
  • 52,135
  • 87
  • 247
  • 375

5 Answers5

16

As discussed in the comments on this answer, setting certain values can have unexpected consequences.

In Javascript, this is more likely because setting a global variable actually means setting a property of the window object. For instance:

function foo (input) {
    top = 45;
    return top * input;
}
foo(5);

This returns NaN because you can't set window.top and multiplying a window object doesn't work. Changing it to var top = 45 works.

Other values that you can't change include document. Furthermore, there are other global variables that, when set, do exciting things. For instance, setting window.status updates the browser's status bar value and window.location goes to a new location.

Finally, if you update some values, you may lose some functionality. If, for instance, you set window.frames to a string, for instance, you can't use window.frames[0] to access a frame.

Community
  • 1
  • 1
lonesomeday
  • 233,373
  • 50
  • 316
  • 318
5

Global variable make it very difficult to isolate your code, and to reuse it in new contexts.

Point #1: If you have a Javascript object that relies on a global var. you will not be able to create several instances of this object within your app because each instance will change the value of the global thereby overwriting data the was previously written by the another instance. (Unless of course this variable holds a value that is common to all instances - but more often than not you'll discover that such an assumption is wrong).

Point #2: Globals make it hard to take existing pieces of code and reuse them in new apps. Suppose you have a set of functions defined in one file and you want to use them in some other file (in another app). So you extract them to a new file and have that file included in the new app. If these function rely on a global your 2nd app will fail at runtime because the global variable is not there. The dependency on globals is not visible in the code so forgetting these variables (when moving functions to new files) is a likely danger.

Itay Maman
  • 30,277
  • 10
  • 88
  • 118
1

They're global variables, so yes, all of the "usual dangers" apply. The main thing that distinguishes them from global variables in other languages is that:

  • You don't explicitly declare them in a global scope. If you mistakenly omit var in a variable declaration, you've accidentally declared a global variable. JavaScript makes it a little too easy to unintentionally declare global variables; contrast this with Scheme, which raises an error if a variable is not defined in the global scope.
  • Global variables, at least in the browser, are aliased by window[variable_name]. This is potentially worrisome. For example, some of your code might access window['foo'] (with the intention of accessing a global variable). Then, if you accidentally type foo instead of var foo elsewhere in the program, you have declared a reference to window['foo'], which you meant to keep separate.
ide
  • 19,942
  • 5
  • 64
  • 106
0

One issue is that you may be trampling on already defined variables and not know it, causing weird side effects in other parts of the code that can be a bear to track down.

Another is that it is just sloppy code. You should not be creating variable with more scope than they need since at the very least it keeps more variables in memory and at worst it can create data scenarios you didn't intend.

The bottom line is that when you do that you don't know for sure that you are not messing up other functions that use a global variable of the same name. Sometimes it isn't even your fault, a lazy programmer of another plugin left something global that was meant to have scope inside of a function. So it is a very practical safeguard for writing better and less buggy code.

Ben
  • 9,725
  • 6
  • 23
  • 28
0

The problems of typical global variables is that they are, well, global - there is no scope to enclose them, and any code that you are executing / interacting with (such as a library that you call down the road) could modify the variable without a warning.

However, these problems are compounded in Javascript by two things:

  1. You can define a global variable anywhere - the only requirement for that is to forget the var keyword.
  2. It is extremely easy to define a global variable when you had no intent to do so. That is the problem that "implied" globals have over "typical" globals - you will create them without even knowing you did.

In a reasonably-designed language that includes truly global variables (ok, so not that reasonably-designed), you would have a limited handful of places to define globals, and it would require a special keyword to do so.

Jean Hominal
  • 16,518
  • 5
  • 56
  • 90