Saturday, August 31, 2013

Previous Interview Question Next Interview Question
Home

Differentiate between “var a=2” and “a =2” ?

The major difference between the two is that one variable is local and the other is global. “var” basically defines the scope of the variable.

When we add var to a variable value assignment, javascript ensures that the variable is confined to whichever function it is assigned to and does not collide with the same name variable within another function.

When we don’t use var, then it is declared as a global function and chances of collision can happen. So it’s always advisable to use “var” before variable value assignment. If needed use an anonymous function for closure.

Previous Interview Question Next Interview Question
Home

0 comments :

Post a Comment