What does “use strict” do in JavaScript, and what is the reasoning behind it?
41
Recently, I ran some of my JavaScript code through Crockford's JSLint, and it gave the following error:

Problem at line 1 character 1: Missing "use strict" statement.

Doing some searching, I realized that some people add "use strict"; into their JavaScript code. Once I added the statement, the error stopped appearing. Unfortunately, Google did not reveal much of the history behind this string statement. Certainly it must have something to do with how the JavaScript is interpreted by the browser, but I have no idea what the effect would be.

So what is "use strict"; all about, what does it imply, and is it still relevant?

Do any of the current browsers respond to the "use strict"; string or is it for future use?
improve this question | comment
Aileen MacGyver Created at: 2013-11-13 17:07:35 UTC By Aileen MacGyver
+1 for cleaning up your writing without strict need for getting your question answered (and for the nice question, of course, I was asking this myself, too)! - Laila Lockman
@Sean Please add "use strict" always! And if it breaks your code, it's a good occasion to become a better programmer and start writing good code. - Alan Kilback
@Jose: in 2009, when I wrote that comment, no browser supported strict mode, and adding "use strict" was a timebomb. See bugzilla.mozilla.org/show_bug.cgi?id=579119 (Amazon screwed up) and bugzilla.mozilla.org/show_bug.cgi?id=593963 (Intel screws up.) Now that real browsers support strict mode, please do use it. (and make sure you test in FF4+ and Safari5.1+) - Princess Oberbrunner
@Sean: right :) Just don't like the fact that people get scared about such a good feature only because they see a high voted comment stating not to use it. - Alexzander Kunze
Until every browser in real use supports strict mode, no validator should ever show a warning for it, and any validator that does is giving such broken, clueless advice that it can't be trusted.  Strict mode takes code that works in all browsers, and turns it into code that works in some and not in others, which is catastrophically worse.  It's only appropriate for testing use, not for production use, or where you know all users will have browsers that support it (eg. if you target specific browsers). - Gaetano Steuber
7 Answers
0
Why should I add code like "use strict"; which is possibly dangerous for my script to be run, and may shut it down for an unimportant reason??

This makes no sense and is a unnecessary overhead, so I strongly suggest only using "use strict" for debugging and afterwards delete it again!

I just read somewhere in the internet:


  In JavaScript, if a variable is not explicitly declared, it is implicitly assigned global scope.This may look fun to code in JS, but is a nightmare to maintain in huge projects. Strict mode throws error if variables are assigned values without declaring and hence implicit global variables will not be created. This is a huge win for the language as well as for huge projects.


Suggestion: only use globals if they're needed, and always use "var" before any other variable. But also don't use "use strict" in any productive environment, because it can implement its own bugs into your code...and in the end preventing runnable code from running in certain browsers!

I'm not the only one saying this: http://www.howtocreate.co.uk/strictJSFirefox.html
0
"Use Strict"; is an insurance that programmer will not use the loose or the bad properties of JavaScript, it is a guide, just like a ruler will help you make straight lines, "Use Strict" will help you do "Straight coding".
Those that prefer not to use rulers to do their lines straight usually end up in those pages asking for others to debug their codes.
Believe me the overhead is negligible compared to poorly designed code.
Doug Crockford whom been a Senior JavaScript developer for several years, has a very interesting post here, personally i like to return to his site all the time to make sure i don't forget my good practice.
Modern JavaScript practice should always evoke the "Use Strict"; pragma.
The only reason that the ECMA Group has made the "Strict" mode optional is to permit less experienced coders access to JavaScript and give then time to adapt to the new and safer coding practices.
0
If people are worried about using use strict it might be worth checking out this article: 

http://www.novogeek.com/post/ECMAScript-5-Strict-mode-support-in-browsers-What-does-this-mean.aspx

It talks about browser support, but more importantly how to deal with it safely:

function isStrictMode(){
    return !this;
} 
//returns false, since 'this' refers to global object and '!this' becomes false

function isStrictMode(){   
    "use strict";
    return !this;
} 
//returns true, since in strict mode, the keyword 'this' does not refer to global object, unlike traditional JS. So here,'this' is null and '!this' becomes true.

0
It's a new feature of ECMAScript5.  John Resig wrote up a nice summary of it.

It's just a string you put in your js files (either at the top of your file or inside of a function) that looks like this:

"use strict";


Putting it in your code now shouldn't cause any problems with current browsers as it's just a string.  It may cause problems with your code in the future if your code violates the pragma.  For instance, if you currently have foo = "bar" without defining foo first, your code will start failing...which is a good thing in my opinion.
0
If you use a browser released in the last year or so then it most likely supports JavaScript Strict mode. Only older browsers around before ECMAScript 5 became the current standard don't support it. 

The quotes around the command make sure that the code will still work in older browsers as well (although the things that generate a syntax error in strict mode will generally just cause the script to malfunction in some hard to detect way in those older browsers).
0
I strongly recommend every developer to start using strict mode now. There are enough browsers supporting it that strict mode will legitimately help save us from errors we didn’t even know where in your code. Apparently, at initial stage there will be errors we have never encountered before. To get full benefit, we need to do a proper testing after switching to strict mode to make sure we have caught everything. Definitely we don’t just throw "use strict" in our code and assume there are no errors. So the churn is that it’s time to start using this incredibly useful language feature to write better code.

eg. 

var person = {
    name : 'xyz',
    position : 'abc',
    fullname : function () {  "use strict"; return this.name; }
};


JSLint is a debugger written by Douglas Crockford. Simply paste in your script, and it’ll quickly scan for any noticeable issues and errors in your code.
0
This article about that might interest you: John Resig - ECMAScript 5 Strict Mode, JSON, and More

To quote some interesting parts:


  Strict Mode is a new feature in ECMAScript 5 that allows you to place a program, or a function, in a "strict" operating context. This strict context prevents certain actions from being taken and throws more exceptions.


And:


  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.
  

Also note you can apply "strict mode" to the whole file... Or you can use it only for a specific function (still quoting from John Resig's article):

// Non-strict code...

(function(){
  "use strict";

  // Define your library strictly...
})();

// Non-strict code... 


Which might be helpful if you have to mix old and new code ;-)

So, I suppose it's a bit like the "use strict" you can use in Perl (hence the name?): it helps you make fewer errors, by detecting more things that could lead to breakages.
Your Answer