Categories
javascript jslint syntax use-strict

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

8226

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?

1

  • 44

    The answers here are old but they are wrong. The main reasoning for strict mode was not to prevent programming errors – it was to make JavaScript lexically scoped so it could be statically analysable :]

    Sep 6, 2019 at 11:40

5350

Update for ES6 modules

Inside native ECMAScript modules (with import and export 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:

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.

Strict mode is now supported by all major browsers.

0

    1381

    It’s a new feature of ECMAScript 5. John Resig wrote up a nice summary of it.

    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.

    0

      804

      The statement "use strict"; instructs the browser to use the Strict mode, which is a reduced and safer feature set of JavaScript.

      List of features (non-exhaustive)

      1. Disallows global variables. (Catches missing var declarations and typos in variable names)

      2. Silent failing assignments will throw error in strict mode (assigning NaN = 5;)

      3. Attempts to delete undeletable properties will throw (delete Object.prototype)

      4. Requires all property names in an object literal to be unique (var x = {x1: "1", x1: "2"})

      5. Function parameter names must be unique (function sum (x, x) {...})

      6. Forbids octal syntax (var x = 023; some devs assume wrongly that a preceding zero does nothing to change the number.)

      7. Forbids the with keyword

      8. eval in strict mode does not introduce new variables

      9. Forbids deleting plain names (delete x;)

      10. Forbids binding or assignment of the names eval and arguments in any form

      11. Strict mode does not alias properties of the arguments object with the formal parameters. (e.g. in function sum (a,b) { return arguments[0] + b;} This works because arguments[0] is bound to a and so on. ) (See examples section below to understand the difference)

      12. arguments.callee is not supported

      [Ref: Strict mode, Mozilla Developer Network]


      Examples:

      1. Strict mode code doesn’t alias properties of arguments objects created within it
      function show( msg ){
          msg = 42;
          console.log( msg );          // msg === 42
          console.log( arguments[0] ); // arguments === 42
      }
      show( "Hey" );
      
      // In strict mode arguments[i] does not track the value of 
      // the corresponding named argument, nor does a named argument track the value in the corresponding arguments[i]
      function showStrict( msg ){
          "use strict";
          msg = 42;
          console.log( msg );          // msg === 42
          console.log( arguments[0] ); // arguments === "Hey"
      }
      showStrict( "Hey" );
      

      1

      • 10

        The example for 11. is unclear, it’s not clear what the difference in strict mode is.

        – RobG

        Mar 11, 2021 at 23:36