Skip navigation

Tag Archives: variable


Global variable in JavaScript is bad. Even if you aware of this, you may end of with some global variable accidentally. Consider the code below

// function definition
function foo(){
  // variable declaration and initialisation
  var a, b = 0;
}

here b is global because

// JavaScript will interpret as follows
var a = (b = 0);

Variable declaration only goes to variable a but not b. So here the variable b becomes global. A better solution would be

// declare both first
var a,b
// assign values after
a=b=0;

Is it helpful? 🙂

Advertisements