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 20;
    }

    // can access variable a
    21;
}

C#
public void b(){
    {
        var 20;
    }

    // can NOT access variable a
    21; // compile time error
}



This scope difference in Javascript may cause hard to catch(..ehem..) bugs and should be taken seriously.

Comments

Popular posts from this blog

Configure hosts File in Android

Does Netflix work on iOS 5 Beta 4?

Space Character Problem on IE 6, 7, and 8