Iris Classon
Iris Classon - In Love with Code

Stupid Question 127: What is strict mode in Javascript?

[To celebrate my first year of programming I will ask a ‘stupid’ questions daily on my blog for a year, to make sure I learn at least 365 new things during my second year as a developer]

Go strict with javascript strict mode

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

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.

Comments

Leave a comment below, or by email.
Mark Carr
1/15/2013 4:09:37 PM
Mmmm. Just catching up on Stupid Questions.POLYGLOT PROGRAMMER: Simple, as the question is implicit regarding the machine (that will function under a set of instructions) and as varied as you can put to the machine [to instructions]. Guess you already know this: It’s a lot more than using the syntax of a formal language, it’s the creative way of using it. So, experiment with languages, their idiosyncrasies, their strengths and weaknesses. After all, it’s writing computer programs. And  if you don’t find it fits your style then write your own compiler/language/library, etc… I# 
Mark
1/15/2013 10:21:41 PM
Isnt strict when you put on the black leather skirt,boots and get out the whips,belts and stuff......?

(Waits for Iris to boot me from twitter and her page )   ;P 


Last modified on 2013-01-15

comments powered by Disqus