views:

9189

answers:

4

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. And 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?

Thanks!

+29  A: 

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.

seth
I'm sorry I didn't pick yours, MARTIN's explanation was just more detailed, even though you were first to the scene with the correct answer. Still I gave you +1.
Mark Rogers
No need to apologize. Thanks for the +1.
seth
+51  A: 

This article might interest you, about that : 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.

Pascal MARTIN
do any of the current browsers respond to the "use strict" string or is it for future use?
Mark Rogers
@m4bwav : I don't think it's well supported : the page about ECMAScript 5 support in Mozilla is quite poor : https://developer.mozilla.org/En/JavaScript/ECMAScript_5_support_in_Mozilla -- and Mozilla's engine is generally not the worse one ^^
Pascal MARTIN
Thanks for the insight!
Mark Rogers
You're welcome ! Have fun!
Pascal MARTIN
“It disables features that are confusing or poorly thought out” — Yikes. Really? So it’s like a “Make the language less crap” option. (You’d hope that would be the default.)
Paul D. Waite
Changing the default after so many years ? Too late for that : it would break so many existing sites/scripts/applications... The only possible thing is to help make things better, for the future.
Pascal MARTIN
+1  A: 

It's a part of ECMAScript 5, John Resig has a post about this in his blog.

svinto
+1  A: 

And there's also this video: Changes to JavaScript, Part 1: EcmaScript 5.

Ionuț G. Stan