My Friend “use strict”
March 11th, 2011At this point you’ve probably been seeing it around a bit, but if you haven’t you should probably start placing
"use strict";
at the top of every js file you write.
So why should you use it?
The big win for me has been catching all the time I fail to define a local variable before assigning to it. For those of you unaware, writing a = ‘b’; when a is undefined used to create a globally scoped variable a. With strict mode, it throws an exception.
In the past, I’ve spent many an hour debugging errors I’ve created by this mistake. Now, I just get exceptions and all is happy.
There are several other ways ‘use strict’ can make your coding life easier, but this feature alone has made it a life saver for me.
In Fact, if you aren’t ready to use it globally in your project yet. you can use it locally at a function level but placing the statement in the first line of the function like so:
function(){ "use strict"; //other code }
What is the best part about “use strict”?
The best part about using “use strict” is that for all practical purposes, browser support doesn’t matter. Compared to all of the other shiny new features emerging for javascript, ‘Strict Mode’ gets the biggest bang for its buck when it’s available for the browser you the developer use. After all, you’re the one creating and testing the bugs it’s going to catch. This is a case where ‘Runs On My Machine’ is almost all you need; ‘Runs On My Bosses Machine’ might be handy as well tho.




Most of us are familiar with Firebug, the powerful & straightforward debugging extension for firefox. You may not be familiar with it’s little brother, Firebug lite.