Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to NOT use "strict mode" in javascript?

I've found this post-What does "use strict" do in JavaScript, and what is the reasoning behind it?

And what I'm understanding here is that I should use strict always.

But I wonder, if it was true, that always is better to use "strict mode", then It wouldn't even exist, because would be a default behavior.

So I searched for ECMAScript 6th edition definition and found that it is the default for a lot of cases.

Accordingly to official documentation about strict mode

An ECMAScript Script syntactic unit may be processed using either unrestricted or strict mode syntax and semantics. The code is interpreted as strict mode code in the following situations:

Global code is strict mode code if it begins with a Directive Prologue that contains a Use Strict Directive (see 14.1.1).

Module code is always strict mode code.

All parts of a ClassDeclaration or a ClassExpression are strict mode code.

Eval code is strict mode code if it begins with a Directive Prologue that contains a Use Strict Directive or if the call to eval is a direct eval (see 12.3.4.1) that is contained in strict mode code.

Function code is strict mode code if the associated FunctionDeclaration, FunctionExpression, GeneratorDeclaration, GeneratorExpression, MethodDefinition, or ArrowFunction is contained in strict mode code or if the code that produces the value of the function’s [[ECMAScriptCode]] internal slot begins with a Directive Prologue that contains a Use Strict Directive.

Function code that is supplied as the arguments to the built-in Function and Generator constructors is strict mode code if the last argument is a String that when processed is a FunctionBody that begins with a Directive Prologue that contains a Use Strict Directive.

ECMAScript code that is not strict mode code is called non-strict code.

So, when is a good choice to use non-strict code?

Thanks in advance.

like image 980
Ramon Marques Avatar asked Jun 16 '17 11:06

Ramon Marques


People also ask

Should you always use strict mode?

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.

Is use strict necessary in JavaScript?

Is use strict necessary? The strict mode is no longer required since the release of ES2015, which fixes most of JavaScript's confusing behavior with a more robust syntax.

What is use strict in JavaScript and when should it be used?

The "use strict" Directive It is not a statement, but a literal expression, ignored by earlier versions of JavaScript. The purpose of "use strict" is to indicate that the code should be executed in "strict mode". With strict mode, you can not, for example, use undeclared variables.

Where should we place use strict?

log(myVariable); // 9 function hello() { // applicable only for this function 'use strict'; string = 'hello'; // throws an error } hello(); If you use 'use strict'; inside a function, the code inside the function will be in strict mode. In the above program, 'use strict'; is used inside the hello() function.


2 Answers

So, when is a good choice to not use strict mode?

When you are running old (or third party) code that you haven't had time to update yet.

Strict mode is simply better. It isn't on by default because it would break old code that was not written with it in mind.

like image 146
Quentin Avatar answered Sep 28 '22 07:09

Quentin


So, when is a good choice to not use strict mode?

Strict mode will throw reference error when found non declared variables and in some cases.

If you have such an unrestrictedly typed code, that is used variables without declaring. One variable declared within some function/scope and used from somewhere else(it will be undeclared there) and you can't rewrite/change them, then you should not go for "use strict;" mode because it will break the code.

From MDN

Strict mode makes several changes to normal JavaScript semantics.

  1. strict mode eliminates some JavaScript silent errors by changing them to throw errors.
  2. 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.
  3. strict mode prohibits some syntax likely to be defined in future versions of ECMAScript.

When in strict mode

You can't just use variables without declaring them as you do in non strict mode

That is

The following will work because it is non strict mode

a = "Hello World"; console.log(a); 

The following won't work because it is in strict mode

'use strict'; a = "Hello World";  // => Throw a reference error console.log(a); 

The above code will throw a reference error because variable a is used without declaring it.

So, you should use

'use strict'; var a = "Hello World"; console.log(a); 

Example in non strict mode

a = "Hello World"; console.log(a);

Example in strict mode with error

'use strict'; a = "Hello World"; console.log(a);

Example in strict mode without error

'use strict'; var a = "Hello World"; console.log(a);

In short

strict mode will bound you with a lot of restrictions but may improve performance as Stated by MDN. also reduce the load of JavaScript Engines.

Tip

If you want to migrate from non strict to strict mode, you should be very careful with each line of code and variables and also their scopes.

like image 34
Sagar V Avatar answered Sep 28 '22 08:09

Sagar V