For a very long time, javascript has remained the same. Browser venders have given us new things to access from using javascript. But the language it’s self has remained pretty much the same since it’s conception in 1995. As of this month however, things have changed. Firefox 4 will soon be released and with it the first browser supporting a strict mode for javascript.
The strict mode is technically not javascript at all. In fact, every time you read the word ‘javascript’ on this website I’m actually referring to ECMAScript. This is the official standard that grew out of javascript. In 2009 the newest version of ECMAScript was completed, ES5. With it came a big bag full of goodies, one of which is a native JSON parser which you might have seen by now. And another is strict mode.
What is strict mode
Strict mode is a ‘best practice’ mode of javascript. The guys behind ES5 figured that the language needed to be backward compatible, if it were to stand a chance of ever being implemented. But equally some of the worst things in the language had to be cut out. Which is where strict mode comes in. Just like strict mode in HTML, strict mode in javascript stops you from using some of the bad features in the language, such as:
- Assigning to undefined variables in a function
- Messing around with eval
- Use delete on things other then properties
- Assign to the arguments object
- Access arguments.caller or arguments.callee
- Use ‘with’ as a shorthand to access properties
It’s quite a list, and there is a lot more. The most important one I find is the first one. With strict mode on, javascript will let you know when you forgot to declare a variable. This is vary important because undeclared variables end up in the global, where they can cause problems you might never know about.
Enabling strict mode
Strict mode is used by including "use strict"; in your javascript. This goes either at the top of your script, or at the top of a function. This last one is very useful because even if your code ends up getting merged and compressed with other scripts on a page, with a function or module in strict mode everything inside it will be considered strict, while all the other scripts are still louse.
Strict mode at the top of a script:
<script> "use strict"; // Strict script here </script>
Strict mode at the top of a function:
function foo() {
"use strict";
// function here
}
Firefox 4 and more
I’ve been using strict mode since the day I found out that one of those FF4 betas had it implemented. It makes for a good browser to run your first tests in. It’s saved a lot of time. Unfortunately Firefox is currently the only browser supporting strict mode. And although it’s true that for most new ES5 goodies we’re going to have to wait until all browsers support them, strict mode can be used today. So let’s all start writing strict code and send browser makers a message.
I definitely wanted to post a remark so as to thank you for these great tactics you are placing at this site. My considerable internet investigation has now been paid with beneficial facts and techniques to talk about with my classmates and friends. I would believe that most of us readers actually are quite blessed to live in a great website with very many awesome individuals with great tips and hints. I feel truly fortunate to have seen the site and look forward to many more enjoyable times reading here. Thanks a lot again for everything.