javascript tutorial - [Solved-5 Solutions] Use strict - javascript - java script - javascript array



Problem:

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

Solution 1:

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 we can use it only for a specific function

// Non-strict code...

(function(){
  "use strict";

  // Define your library strictly...
})();

// Non-strict code... 1
click below button to copy the code. By JavaScript tutorial team

Solution 2:

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 our 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 we currently have foo = "bar" without defining foo first, our code will start failing...which is a good thing in our opinion.

Solution 3:

  • If people are worried about using use strict it might be worth checking out this article:
  • It talks about browser support, but more importantly how to deal with it safely:
function isStrictMode(){
    return !this;
} 
/*
   returns false, since 'this' refers to global object and 
   '!this' becomes false
*/

function isStrictMode(){   
    "use strict";
    return !this;
} 
/* 
   returns true, since in strict mode the keyword 'this'
   does not refer to global object, unlike traditional JS. 
   So here, 'this' is 'undefined' and '!this' becomes true.
*/
click below button to copy the code. By JavaScript tutorial team

Solution 4:

at the initial stage there will be errors we have never encountered before. To get the full benefit, we need to do proper testing after switching to strict mode to make sure we have caught everything. Definitely we don’t just throw use strict in our code and assume there are no errors. So the churn is that it’s time to start using this incredibly useful language feature to write better code.

  • For example,
var person = {
    name : 'xyz',
    position : 'abc',
    fullname : function () {  "use strict"; return this.name; }
};
click below button to copy the code. By JavaScript tutorial team

Solution 5:

  • "Use Strict"; is an insurance that programmer will not use the loose or the bad properties of JavaScript. It is a guide, just like a ruler will help you make straight lines. "Use Strict" will help you do "Straight coding".
  • Those that prefer not to use rulers to do their lines straight usually end up in those pages asking for others to debug their code.
  • Believe me. The overhead is negligible compared to poorly designed code. Doug Crockford, who has been a senior JavaScript developer for several years, has a very interesting post here. Personally, I like to return to his site all the time to make sure I don't forget my good practice.
  • Modern JavaScript practice should always evoke the "Use Strict"; pragma.

Related Searches to javascript tutorial - Use strict