Skip to main content

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?



Thanks!


Source: Tips4allCCNA FINAL EXAM

Comments

  1. 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.

    ReplyDelete
  2. 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.

    ReplyDelete
  3. Another good explanation of strict mode, with plenty of examples:

    ECMAScript 5 strict mode in Firefox 4

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

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

    ReplyDelete
  6. 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).

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex