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?
Best Answer
Update for ES6 modules
Inside native ECMAScript modules (with
import
andexport
statements) and ES6 classes, strict mode is always enabled and cannot be disabled.Original answer
This article about Javascript Strict Mode might interest you: John Resig - ECMAScript 5 Strict Mode, JSON, and More
To quote some interesting parts:
And:
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):
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.Strict mode is now supported by all major browsers.