All In One Script



PHP,HTLM,CSS,Jquery,AJAX,Javascript and etc doubts and sample codes

  • Home
  • Javascript
  • PHP
  • CSS
  • SQL/MYSQL

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

by Blogger 4:00:00 AM Javascript Syntax use-strict

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?

Solution:

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.
Currently, it's supported by all major browsers (bar IE 9 and below).


READ MORE
SHARE :

Search This Blog

Followers

  • Popular
  • Recent
  • Comments
    How to get Real IP,ISP,Country,City and etc from Visitor using PHP
    Get first key in a (possibly) associative array?
    Solved : curl_init() function not working in Ubuntu
    Does finally always execute in Java?
    Serving XHTML and self-closing tags
    How can I make the cursor a hand when a user hovers over a list item?
    How to efficiently iterate over each Entry in a Map?
    Why does this code using random strings print “hello world”?
    In Java, difference between default, public, protected, and private
    Window clearTimeout() Method

Instagram

About

Popular Posts

  • How to get Real IP,ISP,Country,City and etc from Visitor using PHP
    How to get Real IP,ISP,Country,City and etc from Visitor using PHP Php Get Real visiter's IP and ISP and Country and City and Countr...
  • Get first key in a (possibly) associative array?
    Get first key in a (possibly) associative array? What's the best way to determine the first key in a possibly associative array? My...
  • Solved : curl_init() function not working in Ubuntu
    Solved : curl_init() function not working in Ubuntu  Here solved the error  Fatal error: Call to undefined function curl_init() ...
  • Does finally always execute in Java?
    Does finally always execute in Java? I have a try/catch block with  return s inside it. Will the finally block be called? For example...
  • Serving XHTML and self-closing tags
    Serving XHTML and self-closing tags I am trying to follow the xhtml 1.0 strict standard as I am creating my website. Right now, validat...
  • How can I make the cursor a hand when a user hovers over a list item?
    How can I make the cursor a hand when a user hovers over a list item? I've got a list, and I have a click handler for its items: ...
  • How to efficiently iterate over each Entry in a Map?
    How to efficiently iterate over each Entry in a Map? If I have an object implementing the  Map  interface in Java and I wish to iterate...
  • Why does this code using random strings print “hello world”?
    Why does this code using random strings print “hello world”? The following print statement would print "hello world". Could a...
  • In Java, difference between default, public, protected, and private
    In Java, difference between default, public, protected, and private In Java , are there clear rules on when to use each of access modifi...
  • Window clearTimeout() Method
    Window clearTimeout() Method Definition and Usage The clearTimeout() method clears a timer set with the setTimeout() method. ...

statcounter



statcounter



Template Created By ThemeXpose & Blogger Templates