Strict Mode in Microsoft Visual Studio 2012

Don Kiely explains why Visual Studio 2012's strict mode is useful to web developers

When I started thinking about what I would write about for Dev Pro this month, I perused Visual Studio's documentation in the MSDN article "What's New in Visual Studio 2012 RC," since Microsoft had recently unleashed the Visual Studio 2012 Release Candidate (RC) into the world. Scrolling through the list of topics, I found something interesting in the JavaScript section about introducing "additional run-time constraints and error-checking into your code." Now that sounded interesting! Did Microsoft come out with a new JavaScript library, available for development, that would help me write bug-free code? I should have been more wary or cynical when I clicked the link for more information, which took me to the "Strict Mode (JavaScript)" article.

I wondered whether Microsoft was bringing something such as Visual Basic's Option Strict statement to client-side JavaScript code. Would Microsoft give users something even better? Now I was getting excited!

What I found in the "Strict Mode (JavaScript)" article was a description of how to use strict mode, which is defined in the ECMAScript 5 standard specification for what most people know as JavaScript. Admittedly, I was browsing the advanced JavaScript topics in the Visual Studio documentation at that point. But Microsoft had made it seem that this was a great new feature with the article's headline, "What's New in Visual Studio 2012 RC." The article doesn't mention anything about the ECMAScript specification or that strict mode is a feature of JavaScript, not Visual Studio.

When I got over that bit of shock, I realized that a lot of longtime web developers might not have paid enough attention to standard JavaScript to know about strict mode. As a result, strict mode is a topic that's worth writing about. Strict mode is a relatively easy way to let the JavaScript interpreter help you write efficient code that uses only the good parts of the language. For a marvelous treatment of the good parts, you should read, digest, and read again Douglas Crockford's JavaScript: The Good Parts (Yahoo Press, 2008). It's an easy and short read for experienced JavaScript developers, but it's packed full of good advice.

Strict mode removes the language's bad parts but lets the massive existing JavaScript code base, which relies on that bad language, keep working. In essence, strict mode is a smaller language that avoids most of the language's warts and looks to the future because the ECMAScript specification is much more robust and refined. In the meantime, the default non–strict mode of the language is highly compatible with earlier versions, warts and all.

If you're writing for browsers that support ECMAScript 5, you can use strict mode by including a strict statement at the top of your JavaScript code before any other statements. You can apply this statement to entire scripts or individual functions. To implement strict mode in your JavaScript code, type the following lines of code:


            "use strict";

            ... rest of JavaScript code


The "Strict Mode (JavaScript)" article outlines what restrictions are placed on your code during runtime by using strict mode. These restrictions include requiring you to declare a variable before using it, preventing duplicate property names in an object, and not converting the value of this to the global object when it's null or undefined. There are a lot more restrictions in the ECMAScript 5 specification, so it's worth spending some time learning about the changes.

Why do these strict mode matter? Because ECMAScript 5's script mode is the future of JavaScript. It's a chance to correct the errors that Brendan Eich made during the 10 days that he had back in 1995 to put together the new JavaScript language. Strict mode is a way to start moving the language forward again.

This discussion begs the question of which language version you should target: ECMAScript 3, the default ECMAScript 5, or the strict ECMAScript 5? To some extent, your choice is constrained by the browsers that you have to support. As long as the browser versions were released no earlier than the early part of this millennium and they support JavaScript (some mobile browsers don't), you should be safe using ECMAScript 3. As new browser versions adopt ECMAScript 5, use the strict mode of ECMAScript 5. This language version is the safest choice, with features that are largely well-designed, reliable, and can help you write error-free code.

Microsoft isn't solely responsible for strict mode, although the company's representative on the ECMAScript committee might have had a hand in it. It's not a new feature in Visual Studio 2012, except as far as you write JavaScript code in a project that targets any browser, which you can do in any version of Visual Studio. But it's a good thing if Microsoft helps get the word out that strict mode is the way to write efficient JavaScript code, so I can forgive the documentation writers a little literary license!

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.