Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Benefits of "Use Strict" in JS [duplicate]

What are the additional benefits of "use strict" other than preventing bad coding? For instance, does it allow the script to run faster because the interpreter knows the code its optimized?

like image 309
TMacGyver Avatar asked Feb 16 '14 16:02

TMacGyver


People also ask

What is the purpose of use strict in JavaScript?

The use of strict mode: helps to write a cleaner code. changes previously accepted silent errors (bad syntax) into real errors and throws an error message. makes it easier to write "secure" JavaScript.

Should you use strict mode JavaScript?

First, all of your code absolutely should be run in strict mode. Core modern javascript functionality is changed (see . call() and apply()) or disfigured (silent Errors) by executing code outside of strict mode.

What is use strict What are the advantages and disadvantages to using it?

what are the advantages and disadvantages to using it? If you put "use strict"; at the top of your code (or function), then the JS is evaluated in strict mode. Strict mode throws more errors and disables some features in an effort to make your code more robust, readable, and accurate.

Do I need use strict in ES6?

Strict Mode(“use strict”) helps identify common issues (or “bad” parts) and also helps with “securing” JavaScript. In ES5, the Strict Mode is optional but in ES6, it's needed for many ES6 features.


2 Answers

There are a zillion benefits to strict mode, but since you asked specifically about performance, not just the good coding benefits, here's what MDN says about that:

Strict mode makes several changes to normal JavaScript semantics. First, strict mode eliminates some JavaScript silent errors by changing them to throw errors. Second, strict mode fixes mistakes that make it difficult for JavaScript engines to perform optimizations: strict mode code can sometimes be made to run faster than identical code that's not strict mode. Third, strict mode prohibits some syntax likely to be defined in future versions of ECMAScript.

So as you asked, according to the Firefox folks at MDN, strict mode code can sometimes run faster.

For general benefits of strict mode, see What does "use strict" do in JavaScript, and what is the reasoning behind it?

like image 139
jfriend00 Avatar answered Oct 12 '22 14:10

jfriend00


Here are the benefits :)

  1. Duplicate keys in object.
  2. Variables without var
  3. Duplicate arguments
  4. Freezes the arguments of the functions

See a better explanation of Use strict in JavaScript here.

like image 38
msapkal Avatar answered Oct 12 '22 13:10

msapkal