Sign In
×

"Use Strict"

I recently came across this article that explains how to use strict mode in javascript.

Strict mode is enabled by adding the string "use strict" at the top of your file, or inside a function definition.


(function() {  
   "use strict"  
})(); 

Since "use strict" is only a string, your code will not break in older browsers. However, in the modern browsers new exceptions are thrown. For instance, if you wrote foo = "bar", an error would be thrown because you have not defined foo. This is a good thing because it watches out for potential breakage.


John Resig writes:

Strict mode helps out in a couple ways:
  • It catches some common coding bloopers, throwing exceptions.
  • It prevents, or throws errors, when relatively "unsafe" actions are taken (such as gaining access to the global object).
  • It disables features that are confusing or poorly thought out.


tags: javascript,"use strict"

Tue Jul 09 2013 01:06:48 GMT+0000 (UTC) by Michael Reeder