JavaScript block scoping


I'm going through BLOCKED SCRIPT The Good Parts (which I highly, highly recommend), and I'm finding I knew next to nothing about JavaScript.  For example, this does not compile in C#:

public void TestScope()
{
    if (true) 
    {
        int i = 5;
    }

    i.ShouldEqual(5); // does not compile
}

This is because C# has block scope.  Any variable declared inside a block is scoped to that block, but not to any parent blocks.  JavaScript, however, does not have block scope:

var foo = function()
{
    if (true)
    {
        var a = 5;
    }
    
    alert(a);
}

foo();

That will display "5".  Since there is no block scope, the book recommends declaring all variables at the top of the function.  JavaScript does have function scope (as well as closures), so this is the safer bet:

var foo = function()
{
    var a;
    
    if (true)
    {
        a = 5;
    }
    
    alert(a);
}

foo();

This includes any variables declared inside for loops and any other kind of block.  Declaring them at the top of the function will eliminate pesky scoping bugs, when you might accidentally revert back to C-style block scope rules.

Did I also mention you should pick up BLOCKED SCRIPT The Good Parts?  It turns out learning about the language may dispel ill feelings towards it.


Posted Sep 26 2008, 12:03 PM by bogardj
Filed under:

Comments

Paul Batum wrote re: JavaScript block scoping
on 09-26-2008 5:06 PM

Jimmy you just cost me £114.66!!

The problem with Amazon is you never manage to check out with just one book in your basket..

bogardj wrote re: JavaScript block scoping
on 09-26-2008 10:21 PM

@Paul

You're welcome.

Scott wrote re: JavaScript block scoping
on 09-26-2008 11:05 PM

BLOCKED SCRIPT  is even weirder than that. What do you think will be the value of myVar when you call the function?

function t()

{

alert(myVar);

var myVar = "scope";

alert(myVar);

};

The first alert shows "undefined". The second alert shows "myVar".

Weird huh? No error when you try to access a variable that hasn't been declared yet.

Dew Drop - September 27, 2008 | Alvin Ashcraft's Morning Dew wrote Dew Drop - September 27, 2008 | Alvin Ashcraft's Morning Dew
on 09-27-2008 6:58 AM

Pingback from  Dew Drop - September 27, 2008 | Alvin Ashcraft's Morning Dew

Sergio Pereira wrote re: JavaScript block scoping
on 09-27-2008 9:01 AM

That book is nice indeed. It's short and filled with valuable information. Great for anyone that does not remember why they hate "blocked script"

Arjan`s World » LINKBLOG for September 27, 2008 wrote Arjan`s World » LINKBLOG for September 27, 2008
on 09-27-2008 4:08 PM

Pingback from  Arjan`s World    » LINKBLOG for September 27, 2008

Bill Barry wrote re: JavaScript block scoping
on 09-27-2008 7:57 PM

This is why you should always consider a good lint program:

like http://www.jslint.com/

Add a Comment

(required)  
(optional)
(required)  
Remember Me?

Enter the numbers above:
Copyright Los Techies 2008, 2009. All rights reserved.
Powered by Community Server (Commercial Edition), by Telligent Systems