Sobes.tech
Middle

What is strict mode in JavaScript? Do you know the difference between script and module?

sobes.tech AI

Answer from AI

Strict mode in JavaScript is a mode that introduces stricter rules for code, helping to avoid errors and unsafe constructs. It is activated by the directive 'use strict'; at the beginning of a script or function.

In strict mode, things like:

  • using undeclared variables,
  • duplicating function parameter names,
  • assigning values to read-only properties,
  • deleting undeclared variables, and others are prohibited.

Difference between script and module:

  • script — a regular JavaScript file executed in the global context. Strict mode is not enabled by default, and must be explicitly activated.

  • module — an ES6 module, which runs in strict mode by default. Modules have their own local context, do not pollute the global object, and support import and export.

Example:

<script src="script.js"></script> <!-- regular script -->
<script type="module" src="module.js"></script> <!-- module -->

In a module, strict mode is automatically enabled, whereas in a regular script it is not.