Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global variables vs. passing a value into a function?

I'm new to JavaScript, and have a simple (I presume) question regarding best practices for accessing variables in functions:

When should I declare a global variable, as opposed to simple passing a value into a function?

like image 627
Nathan Arthur Avatar asked Sep 21 '11 21:09

Nathan Arthur


People also ask

Why is parameter passing better than global variables?

Parameter passing - allows the values of local variables within the main program to be passed to sub-programs without the need to use global variables. The value of these variables (or a copy of the value of these variables) is passed as a parameter to and from sub-programs as necessary.

What is a global variable and what is a better way to pass information into a function?

Global variables are variables that are accessible regardless of scope. Traditionally, functions operate within a local scope with limited access to variables that are not passed as a parameter. Setting a variable as global breaks the rules of encapsulation so that the specified variable is more accessible.

Why should you avoid using global variables?

Using global variables causes very tight coupling of code. Using global variables causes namespace pollution. This may lead to unnecessarily reassigning a global value. Testing in programs using global variables can be a huge pain as it is difficult to decouple them when testing.

Can global variables be used in functions?

Global variables can be used by everyone, both inside of functions and outside.


2 Answers

Declaring a global variable should only be used as an option of last resort.

Global variables are bad in general and especially so in javascript. There is simply no way to prevent another piece of javascript from clobbering your global. The clobbering will happen silently and lead to runtime errors.

Take the following as an example.

// Your code
myParam = { prop: 42 };
function operateOnMyParam() {
  console.log(myParam.prop);
}

Here i've declared 2 global variables

  • myParam
  • operateOnMyParam

This might work fine while testing your javascript in isolation. However what happens if after testing a user combines your javascript library with my javascript library that happens to have the following definitions

// My code
function myParam() {
  console.log("...");
} 

This also defines a global value named myParam which clashes with your myParam. Which one wins depends on the order in which the scripts were imported. But either way one of us is in trouble because one of our global objects is dead.

like image 58
JaredPar Avatar answered Sep 29 '22 19:09

JaredPar


There are many, many reasons.. but an easy one is.. The argument of a function only exists in the function, while it's running. A global variable exists all the time, which means:

  • it takes up memory until you manually 'destroy' it
  • Every global variable name needs to be unique
  • If, within your function.. you call another function.. which ends up calling the first function, all of a sudden you may get unexpected results.

In short: because the function argument only lives for a really short time and does not exist outside the function, it's much easier to understand what's going on, and reduced the risk of bugs greatly.

like image 21
Evert Avatar answered Sep 29 '22 19:09

Evert