Skip navigation

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: