"use strict" is Strict Mode
Tags: javascript, strictmode, beforees6
Published: 2021-09-20 17:47:44

Inside native ECMAScript modules (with import and export statements) and ES6 classes, strict mode is always enabled and cannot be disabled.

So, the first thing to know: 

* 1. __Nowadays we use Strict Mode everyday.__

Strict Mode is now supported by all major browsers.

The syntax, for declaring strict mode, was designed to be compatible with older versions of JavaScript to has no side effects. So `"use strict";` only matters to new compilers that "understand" the meaning of it. 

Strict mode code and non-strict mode code can coexist, so scripts can opt into strict mode incrementally.

So, the second thing to know: 

* 2. __`"use strict";` is ignored by earlier versions of JavaScript.__

The `"use strict"` directive was new in ECMAScript version 5.

ECMAScript 2009, also known as ES5, was the first major revision to JavaScript.

Strict mode isn't just a subset: it intentionally has different semantics from normal code.

Strict mode changes both syntax and runtime behavior. 

JavaScript was designed to be easy for novice developers, and sometimes it solves operations which should be semantic errors into non-error operations. Sometimes this fixes the immediate problem, but sometimes this creates worse problems in the future.

Strict mode treats these mistakes as errors so that they're discovered and promptly fixed.

So, the third thing to know: 

* 3. __Strict Mode is the beginning of modernity.__

It states the before and after into javascript paradigm. It is the base of the current javascript programming basics.

1. Changes converted mistakes into errors (as syntax errors or at runtime).

2. Changes simplifyed how the particular variable for a given use of a name is computed.

3. Changes make it easier to write "secure" JavaScript.

4. Changes anticipating future ECMAScript evolution.


## Time travel to the past
There was a time when such amazing things were possible, like:

* you could use undeclared variables (wtf! 😳)
```
// set a variable without the 'var' keyword
x = 3.14;

function myFunction() {
  y = 3.14;
}
```

* Mistyping a variable name creates a new global variable

* not unique function parameter names (wtf! 🤨)
```
// available through 
arguments[i]
```

* declare functions inside control structures
```
if (true) {
  function f() { }
  f();
}

for (var i = 0; i < 5; i++) {
  function f2()
  f2();
}
```

* eval() were allowed to create variables
```
eval ("var x = 2");
alert (x);
```

* In normal mode `this` would return the global object (window):
```
function myFunction() {
  alert(this);
}
myFunction();
```


* `delete` keyword usuallly deletes
```
// variable/object/function/property
var x = 3.14;
delete x;
```

## Back to the Future
In strict mode a non-existing variable/object, any assignment to a non-writable property and etc... will throw an error.

Strict mode changes previously accepted "bad syntax" into real errors.

Strict mode makes it easier to write "secure" JavaScript.

So, the third fourth to know: 

* 4. __Strict Mode move the language to a new programming style.__

In general, the changes enabled by strict mode are all for the better.

Improving its resiliency make it easier to understand what's going on when there are problems.

JavaScript did not have exception handling until ECMAScript 3.

JavaScript’s creator, Brendan Eich, had no choice but to create the language very quickly from several programming languages: Java (syntax, primitive values versus objects), Scheme and AWK (first-class functions), Self (prototypal inheritance), and Perl and Python (strings, arrays, and regular expressions).

On one hand, JavaScript had quirks missing quite a bit of functionality. On the other hand, it had several powerful features that allowed you to work around these problems.

Given its influences, JavaScript enables a programming style that is a mixture of functional programming (higher-order functions; built-in map, reduce, etc.) and object-oriented programming (objects, inheritance).

So, the fiveth and last to know: 

* 4. __Strict Mode breaks from the initial scheme and possibilities into more formal OOP.__

You can use strict mode in all your programs. The "use strict" directive defines that the JavaScript code should be executed in "strict mode". `"use strict"` is just a string expression. Old browsers will not throw an error if they don't understand it.

### Declaring Strict Mode
Strict mode is declared by adding `"use strict";` to the beginning of a script or a function.

Declared at the beginning of a script, it has global scope (all code in the script will execute in strict mode):
```
"use strict";
x = 3.14;  // throw error
var x = 3.14;  // OK
```

Declared inside a function, it has local scope (only the code inside the function is in strict mode):
```
function myFunction() {
  "use strict";
  y = 3.14;   // throw error
  var y = 3.14;   // OK
}
```

Declare local or global `"use strict"` will throw an error feedback by any not legal assignment.


## Conclusion
Nowadays we use Strict Mode everyday because inside native ECMAScript modules (with import and export statements) and ES6 classes, strict mode is always enabled and cannot be disabled.

Strict mode code and non-strict mode code can coexist. In general, the changes enabled by strict mode are all for the better.

Strict mode changes both syntax and runtime behavior and previously accepted "bad syntax" changes into real errors.

Strict mode treats these mistakes as errors or "bad syntax" to they're discovered and promptly fixed.

`"use strict"` is just a string expression. Old browsers will not throw an error if they don't understand it.

The syntax, for declaring strict mode, was designed to be compatible with older versions of JavaScript to has no side effects. So `"use strict";` only matters to new compilers that "understand" the meaning of it.

Declared at the beginning of a script (global scope)
```
// global scope - (on top)
"use strict";

// local scope - inside block
function myFunction() {
  "use strict";
  [...]
}
```