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.
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? 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.
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.
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.
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.
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.
- strict mode eliminates some JavaScript silent errors by changing them to throw errors.
- 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.
- strict mode prohibits some syntax likely to be defined in future versions of ECMAScript.
strict mode
You can't just use variables without declaring them as you do in non strict mode
That is
a = "Hello World"; console.log(a);
'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);
a = "Hello World"; console.log(a);
'use strict'; a = "Hello World"; console.log(a);
'use strict'; var a = "Hello World"; console.log(a);
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With