I was asked this question by a developer just getting started with web development, he has been working with client applications only in C# for a few years and just got started with Web development and at the same time some Windows Store App development with JS. I was going through some basics when he noticed the ‘use strict’ declaration at the top of a file and asked what it was. It occurred to me that it actually would be a great question, so here goes, what is ‘strict mode’?
Strict mode was introduced with ECMAScript 5 (ES5), and it basically forces us to write better code as it wont let things that might cause problems later down the line slip by. Sounds familiar, and you are a C# developer? It reminds me of the warning levels that you can set when programming in C#. The usual recommendation is to also set treat all warnings as errors. Basically it does the same as ‘use strict’ does, it makes sure you don’t use error prone code.
‘Use strict’ can be set at global level, or function level, recommendation is to set it at global level unless you aren’t able to do so.
It adds quite a few restrictions, the most important ones are listed on MSDN in detail, such as not letting you use a variable without declaring it, writing to a read-only property, duplicating a property or parameter name, reserves some words as future keywords, this if null or undefined won’t be converted to the global object, you cannot change arguments inside a function and with is not allowed. There are plenty more, so check it out.
strict mode browser support
In all (okey, ALMOST all) modern browsers it is supported, but be aware that only from IE10 in regards to IE. (A lot has improved in IE10) When not supported the declaration will be interpreted as an unassigned string.
Last modified on 2013-01-15