Let's try to avoid global variables

On the whole global variables are bad, and unintentional global variables can be terrible.

programmers.stackexchange.com/qu … te-so-evil

So can we please avoid create variables outside of constructors - not this in the top level of the code

var iAmGlobal=[];

This will immediately clash with anybody else creating a variable in the top level with this name anywhere else. It will also make a variable called iAmGlobal available in scope for everything (its global).

Surprisingly something like this can also create a global variable

MyThing.prototype.myMethod = function() { var myNumbers = [0,1,2,3,4,5,6,7,8,9]; for (vari=0;i<10;i++) { myNumber=myNumers[i]; } }

the variable ‘myNumber’ is actually defined in the global scope and will clash with any other variable call myNumber anywhere else.

If you put ‘use strict’; at the top of your code (like I have been trying to introduce) then the second example is actually an error and will not work - which is a good thing :slight_smile:

Anyway can we please try to avoid global variables.