Strict mode

Strict mode

October 17, 2022 - 1 minute read

The strict mode introduces a more restricted variant of JavaScript into your module, function or classes. It's an opt-in mechanism and it doesn't have to be used within the whole application - although it's recommended. You can turn in on e.g. in one function when the rest can still be written in a "normal" JavaScript. It may be helpful if you want to migrate a legacy project.

The mode makes several changes to normal JavaScript semantics:

  • introduces early errors - throws an error for errors that aren't strictly syntax ones but are thrown at compile time. Such errors without the mode might be treated as a mistake without an error so the program runs but it may create worse problems in the future. E.g.⁠

⁠```js ⁠'use strict'; ⁠let variable; ⁠vriable = 10; // There's a mispelling so this variable doesn't exist. Without the strict mode on, this variable would be created but in this case a ReferaceError is thrown preventing us from a bug ⁠`

  • speeds up the code - it fixes mistakes that may have impact on JavaScript engines optimalizations
  • make the code more secure

Strict mode is by default turned on for ES6 modules and classes.

Related Posts

Categories