35

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.

Chirag Patel
  • 373
  • 5
  • 20
Ramon Marques
  • 3,046
  • 2
  • 23
  • 34
  • 5
    it cannot be default in browsers, because there is still a lot of application over internet written in old version of javascript and if given browser would introduce `use strict` as default it would broke those applications (legacy code). For all new applications it's recommended to use `use strict`, as you said, always. :) – Kasia Jun 16 '17 at 11:09
  • Just use strict. Older browsers will ignore `use strict` if not supported. There is no, when not to use. – Red Jun 16 '17 at 11:16
  • 1
    `use strict` is here to offer a restrained/safer experience (hence strict). You can use it if you want, don't use it if you don't wanna use it. – Vivick Jun 16 '17 at 11:22

4 Answers4

34

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.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
13

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.

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
Sagar V
  • 12,158
  • 7
  • 41
  • 68
4

As per the MDN link below :-

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Strict_mode

Strict mode in browsers

The major browsers now implement strict mode. However, don't blindly depend on it since there still are numerous Browser versions used in the wild that only have partial support for strict mode or do not support it at all (e.g. Internet Explorer below version 10!). Strict mode changes semantics. Relying on those changes will cause mistakes and errors in browsers which don't implement strict mode. Exercise caution in using strict mode, and back up reliance on strict mode with feature tests that check whether relevant parts of strict mode are implemented. Finally, make sure to test your code in browsers that do and don't support strict mode. If you test only in browsers that don't support strict mode, you're very likely to have problems in browsers that do, and vice versa.

Abhishek Sharma
  • 1,420
  • 1
  • 20
  • 30
2

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

Adding to the other answers, if your javascript code is generated or compiled by a third party library. You don't want to manipulate with it because you didn't write it.

amiramw
  • 506
  • 2
  • 10
  • 2
    If the code is generated, it will be generated with or without `"use strict"`, and you don't get to decide it anyway? – Bergi Oct 01 '17 at 18:28