Function Scope versus Block Scope

I was reading away at some blogs I found online and ran into this interesting site on JavaScript called Common JavaScript “Gotchas” . There are several interesting finds that he shares with everyone, but one stuck out with me so much that I wanted to post it online to spread it to others that may not have known this. JavaScript uses what’s known as function scope in it’s language. This instead of what many languages do called block scope. You may be wondering what this means.

Function Scope

What function scope means, is pretty intersting in concept if you are used to other programming languages (especially OOP). Function scope states that any variable defined in a function is accessible and writable regardless of where in the function the variable is called or set. So what does this mean? Well, if you have something like:

    var fun = "I am great";
    
    var scopeFunc = function(x) {
    
        fun = "Nothing to see here";
        if( x <10)
          fun = "This is cool"

    }
        return fun;

  };

var data = scopeFunc(5);


 

So the question here becomes, does fun stay as it was, or does it change? The answer is it turns to “This is cool”. Why you say? Because the fun variable is never defined inside the if statement (using var) even though it is inside an if statement. This definition is true for the whole function. This is what’ known as variable hoisting. I’m not  sure how I feel about this.

This definitely brings a new light to thoughts about JavaScript. There are people that find this to be the best thing since sliced bread. Me on the other hand, see it as an easy way to have bad practices and unreadable code, but what do I know right?

 

Happy Coding!

Advertisements

About Gregg Coleman

I am Senior-level Software Engineer working primarily these days with .NET. I have a good working knowledge of ASP.NET MVC, Web Forms, WCF web services and Windows Services. I spend much of my time in the Web Services (SOAP and REST) world in my current job designing and implementing various SOA architectures. I have been in the software engineering industry for about 6 years now and will not now nor ever consider myself an "expert" in programming because there is always so much to learn. My favorite thing about designing software is there are always new emerging technologies and something to learn every day! My current job has me spending much of my job on the bleeding edge of technologies and changing gears all the time, so I'm never bored and always challenged. On my spare time I enjoy weight training, reading and venturing to new places near by. Of course programing and learning new technologies are another hobby of mine.
This entry was posted in ASP.NET, C#, JavaScript and tagged , , , , . Bookmark the permalink.

2 Responses to Function Scope versus Block Scope

  1. Borut Levart says:

    You’re mistaken I think. Had you omit var in the if statement, then the main, outer fun would be overwritten. But stating var anywhere within scopeFun makes its fun local and does not reset the outer fun when the function evaluates.

    • @Borut – Thank you for the comments and visiting my site. I see what you are saying and are absolutely correct. I am still trying to get a better understanding of these gotchas in JavaScript, so it’s great to get some information from other developers.

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s