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

3 Must Haves of a Status Report

AWS encryption chart (SSE-S3 vs SSE-KMS vs SSE-C)

Curl "No status code specified in the response"