“use strict” support coming in Firefox 4

One of the bugbears in JavaScript until recently has been that there’s no support to check for undeclared variables explicitly and call them out at interpret time. This can make it somewhat easy to slip up and mis-type an identifier. However, obviously borrowing from Perl, support for a use strict pragma is coming in ECMAScript 5.

What does this mean in practice? Well, I tried a few tests with this quick script I whipped up (the use of Dojo was optional, of course, but I just love using it as a framework):

        <div id="showMe">
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/dojo/1.5/dojo/dojo.xd.js" djConfig="parseOnLoad: true"/></script>
	"use strict";

        dojo.addOnLoad(function() {
		dojo.byId("showMe").innerHTML = "Test passed - use strict works!";
		invalidVariable = 3;
		dojo.byId("showMe").innerHTML = "Test failed - use strict doesn't work!";

The test fails in Chromium 7.0.517.44 and Firefox 3.6.2. However, in the latest Firefox 4 beta 8, it passes – and Firebug very clearly shows the error that’s thrown:

This is great news, and means that you’ll soon be able to start using this pragma as it finds its way into the latest browsers. It’s probably unwise to start including it now – unless you plan to test all your code on a browser that supports it – because of the risk of undetected side effects (for example, a mis-typed variable that also happens to be dead code). But when, for example, Firefox 4 is released, it will hopefully become a standard best practice for web development.