I've Heard Global Variables Are Bad, What Alternative Solution Should I Use?

Posted by Jay on Stack Overflow See other posts from Stack Overflow or by Jay
Published on 2010-04-10T12:11:30Z Indexed on 2010/04/10 12:23 UTC
Read the original article Hit count: 516

I've read all over the place that global variables are bad and alternatives should be used. In Javascript specifically, what solution should I choose.

I'm thinking of a function, that when fed two arguments (function globalVariables(Variable,Value)) looks if Variable exists in a local array and if it does set it's value to Value, else, Variable and Value are appended. If the function is called without arguments (function globalVariables()) it returns the array. Perhaps if the function is fired with just one argument (function globalVariables(Variable)) it returns the value of Variable in the array.

What do you think? I'd like to hear your alternative solutions and arguments for using global variables.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about global-variables