The perils of the missing javascript block scope

Javascript variable scope is something of a strange thing. You have a global and a function scope. But unlike most other languages, there is no block scope! This is often a cause for hard to find bugs.

Let me show you the consequences of this with a small puzzler:

<html>
<body>
<a id="link1" href="">Link 1</a> <span id="part1"></span><br/>
<a id="link2" href="">Link 2</a> <span id="part2"></span><br/>
<a id="link3" href="">Link 3</a> <span id="part3"></span><br/>
<a id="link4" href="">Link 4</a> <span id="part4"></span><br/>
<a id="link5" href="">Link 5</a> <span id="part5"></span><br/>
No Link 6 <span id="part6"></span><br/>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
for (i = 1; i <= 5; i++) {
$("#link"+i).click(function() {
$("#part"+i).html("Clicked !");
return false;
});
}
});
</script>
</body>
</html>


Now what does this do? (you need a bit of jquery knowledge to guess it)

At first glance, it walks over the 5 links present on this page, and registers a handler for mouse clicks that adds the text "Clicked !" to the span element with the same number as the link.

Or does it?

Well, this is what the output looks like when you click on "Link 3":

Test


It is totally wrong ! Now why is that? Well, as we have no block scope, by the time we are done with the for loop, the variable i has been incremented up to 6. It is however still in scope, and therefore this last value gets used when the closure for the click event gets called. And so it is part 6 that receives the new text !

So how can we fix this?

We have to separate the scopes, and to do that we need a new function. This is what the correct code looks like:

<html>
<body>
<a id="link1" href="">Link 1</a> <span id="part1"></span><br/>
<a id="link2" href="">Link 2</a> <span id="part2"></span><br/>
<a id="link3" href="">Link 3</a> <span id="part3"></span><br/>
<a id="link4" href="">Link 4</a> <span id="part4"></span><br/>
<a id="link5" href="">Link 5</a> <span id="part5"></span><br/>
No Link 6 <span id="part6"></span><br/>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
for (i = 1; i <= 5; i++) {
addClickEvent(i);
}
});

function addClickEvent(i) {
$("#link"+i).click(function() {
$("#part"+i).html("Clicked !");
return false;
});
}
</script>
</body>
</html>


With the registration of the click event taking place in a separate function (and therefore a separate scope for the variable i), this is what the output looks like:

Test OK


Which is much more like what we were expecting !

So beware of the lack of block scope in Javascript! It can come and bite more easily than you think if you're not extra careful about it!

 


Axel

About Axel Fontaine

Axel Fontaine is the founder and CEO of Boxfuse the easiest way to deploy JVM and Node.js applications to AWS.

Axel is also the creator and project lead of Flyway, the open-source tool that makes database migration easy.

He is a Continuous Delivery and Immutable Infrastructure expert, a Java Champion, a JavaOne Rockstar and a regular speaker at many large international conferences including JavaOne, Devoxx, Jfokus, JavaZone, QCon, JAX, ...

You can follow him on Twitter at @axelfontaine

 

Architecting for Continuous Delivery and Zero Downtime

Two day intensive on-site training with Axel Fontaine

Upcoming dates

Iasi, Romania (May 10-11, 2017)
Oslo, Norway (Oct 16-17, 2017)

« Taming the beasts: Getting Ubuntu Server 9.10 to run in Virtual PC on Windows 7
One Minute Book Review: Crossing the Chasm »
Browse complete blog archive
Subscribe to the feed