Variable Scope C# vs Javascript
Importantly, variable scope definition of Javascript is so much different than C#'s.
Javascript uses function scope function(){} whereas C# uses block scope {}. Therefore in Javascript, once a variable is defined inside a function, interpreter automatically moves the declaration of variable to the top of the function.
Here is an example;
Javascript
function b(){
{
var a = 20;
}
// can access variable a
a = 21;
}
var a = 20;
}
// can access variable a
a = 21;
}
C#
public void b(){
{
var a = 20;
}
// can NOT access variable a
a = 21; // compile time error
}
This scope difference in Javascript may cause hard to catch(..ehem..) bugs and should be taken seriously.
Comments