Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

What does “use strict” do in JavaScript, and what is the reasoning behind it?

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?
by

2 Answers

kshitijrana14
It's a new feature of ECMAScript 5.
It's just a string you put in your JavaScript 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.
sandhya6gczb
The "use strict" mode instructs the browser to use strict mode which is a reduced safe feature of javascript.
Some features of strict mode are
Disallows global variable
All property name in the object should be unique.
Forbid the With keyword

Login / Signup to Answer the Question.