Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

What does “use strict” do in JavaScript and why should we use it?

Let's discuss the use of "use strict" now a days every other JS API have been using it and we all should know also.

user-image
Question ajoutée par Mohammad Arif , Principal Engineer UI , NatWest Group
Date de publication: 2013/07/01
Fadi Alkhateeb
par Fadi Alkhateeb , Senior Front End Developer , NexTwo

This article can help you about it: http://www.nczonline.net/blog/2012/03/13/its-time-to-start-using-javascript-strict-mode/

Mohammed Rizwan Afgani
par Mohammed Rizwan Afgani , Software Engineer, Team Lead , Prosoft e-Solutions (I) Pvt Ltd

Strict Mode is a new feature in ECMAScript5 that allows you to place a program, or a function, in a “strict” operating context.
It is a way to introduce better error-checking into your code.
When you use strict mode, you cannot, for example, use implicitly declared variables, or assign a value to a read-only property, or add a property to an object that is not extensible.
The restrictions are listed in the Restrictions on Code in Strict Mode (http://msdn.microsoft.com/en-us/library/ie/br230269%28v=vs.94%29.aspx#rest).
It can be applied to the whole file, Or you can use it only for a specific function.
Strict mode helps out in a couple ways:1).
It catches some common coding bloopers, throwing exceptions.2).
It prevents, or throws errors, when relatively "unsafe" actions are taken (such as gaining access to the global object).3).
It disables features that are confusing or poorly thought out.
Hope this answer is helpful

Mohammad Arif
par Mohammad Arif , Principal Engineer UI , NatWest Group

Essentially we should use it to write better JavaScript, Nicholas article is great, he explained things in detail pretty well.
Now every other tool's like JSHint/JSLint gives you the same error if you don't use 'use strict' unless until, you turn it off.
+1 for Fadi/Rizwan.

More Questions Like This