Recently, I ran some of my JavaScript code through Crockford\'s JSLint, and it gave the following error:
Problem at line 1 character 1: Missing \"use
A word of caution, all you hard-charging programmers: applying "use strict"
to existing code can be hazardous! This thing is not some feel-good, happy-face sticker that you can slap on the code to make it 'better'. With the "use strict"
pragma, the browser will suddenly THROW exceptions in random places that it never threw before just because at that spot you are doing something that default/loose JavaScript happily allows but strict JavaScript abhors! You may have strictness violations hiding in seldom used calls in your code that will only throw an exception when they do eventually get run - say, in the production environment that your paying customers use!
If you are going to take the plunge, it is a good idea to apply "use strict"
alongside comprehensive unit tests and a strictly configured JSHint build task that will give you some confidence that there is no dark corner of your module that will blow up horribly just because you've turned on Strict Mode. Or, hey, here's another option: just don't add "use strict"
to any of your legacy code, it's probably safer that way, honestly. DEFINITELY DO NOT add "use strict"
to any modules you do not own or maintain, like third party modules.
I think even though it is a deadly caged animal, "use strict"
can be good stuff, but you have to do it right. The best time to go strict is when your project is greenfield and you are starting from scratch. Configure JSHint/JSLint
with all the warnings and options cranked up as tight as your team can stomach, get a good build/test/assert system du jour rigged like Grunt+Karma+Chai
, and only THEN start marking all your new modules as "use strict"
. Be prepared to cure lots of niggly errors and warnings. Make sure everyone understands the gravity by configuring the build to FAIL if JSHint/JSLint
produces any violations.
My project was not a greenfield project when I adopted "use strict"
. As a result, my IDE is full of red marks because I don't have "use strict"
on half my modules, and JSHint complains about that. It's a reminder to me about what refactoring I should do in the future. My goal is to be red mark free due to all of my missing "use strict"
statements, but that is years away now.