JavaScript: Strict Mode support in Browsers and Backward Compatibility

Strict Mode was introduced in ECMAScript 5.0 and is intended to make programs simpler and free of errors. In strict mode, a more rigid set of syntax checks is used to either catch an undeclared variable or any other errors in your code. Strict mode changes semantics!

What is ECMAScript?

ECMAScript was initiated by a European Standards Body called ECMA to create a formal standard for the JavaScript language. JavaScript is based on the ECMAScript standard (ES). The latest version is based on ECMAScript standard 262 when version 5.0 was approved in December 2009.

There are a number of keywords reserved by JavaScript but here are some reserved when using the ‘strict mode’ - implements, interface, let, package, private, protected, public, static, yield. Apart from these, you cannot use arguments and eval as functions or parameters names. There are many other changes that have been documented here

What if my browser, especially older versions, does not support Strict Mode?

StrictMode is declared as a string. For eg:

Globally as "use strict";

and in a function as

function someFunction(){
"use strict";
// code from DevCurry.com 
}


Since the Strict mode is declared as a string, it is backward compatible. Older browsers simply ignore it, without raising any errors. However you should make sure to test your code in browsers that do and do not support strict mode. 

Which browsers support Strict Mode?

As of this writing IE 10 (Preview 1), Firefox 4 & 5, Chrome 12, Webkit, Besen and Jurassic support Strict Mode. It’s here to stay, so get used to it!


Will you give this article a +1 ? Thanks in advance


About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal

No comments: