Why is there "use strict" in JS/TS

This article is shared from Huawei Cloud Community "What is the "use strict" strict mode in JS/TS? , by gentle_zhou.

In the daily JS/TS project development process, you often see the "use strcit"words at the beginning of the file. What does "use strict" mean here?

To explain it literally, this project file is run under strict conditions. So what are the strict conditions? Why do we need strict conditions?

Navigation of this article: What is strict mode; why do we need strict mode; examples of strict mode restrictions; reference links

What is strict mode

"use strcit"was added in JavaScript 1.8.5 (ECMAScript5) version:

"use strcit"Not a statement, but a declaration with a different meaning than normal code; we "use strcit"declare the use of strict mode by adding an expression to the head of a script or function. The meaning of strict mode allows the code in the project to be executed under strict conditions, which can limit some bad coding habits and expose problems in the coding stage.

Browsers that support strict mode are: Internet Explorer 10+, Firefox 4+ Chrome 13+, Safari 5.1+, Opera 12+.

How to view the error message in the browser? We can press the F12 button or click the three dots in the upper right corner of the browser page - more tools - developer tools to enable debugging mode:

Then click the console tab to view the error message:

Why do we need strict mode

The opposite mode to strict mode is sloppy mode, which, as we can see from the name, is casual and sloppy in its response to the process of writing code. Sloppy mode is not an official name, as long as we don't declare it with strict mode, then the js/ts file is in sloppy mode.

So why do we use strict mode (with additional declarations) instead of sloppy mode directly?

Strict mode can eliminate some unreasonable and imprecise places in JS/TS syntax, and can make JS/TS develop in a more reasonable, safer and more rigorous direction:

  • By changing some silent errors of JS/TS to throwing errors, some silent errors of JS/TS are eliminated, and the security of code operation can be more effectively guaranteed;
  • Improve compiler efficiency and increase running speed;
  • Some syntaxes that may be defined in future versions of ECMAScript are prohibited.


Some of the silent errors are shown in the Strict Mode Restriction Examples section below.

Strict Mode Restricted Examples

  1. Undeclared variables are not allowed


    In debug mode it will show " Uncaught ReferenceError: x is not defined" the
    correct expression should belet x = 520;
  2. Deletion of variables or objects is not allowed


    Uncaught SyntaxError: Delete of an unqualified identifier in strict mode." " will be displayed in debug mode
  3. Delete function is not allowed


    Uncaught SyntaxError: Delete of an unqualified identifier in strict mode." " will be displayed in debug mode
  4. Variable names are not allowed


    Uncaught SyntaxError: Duplicate parameter name not allowed in this context." " will be displayed in debug mode
  5. Octal not allowed


    Uncaught SyntaxError: Octal literals are not allowed in strict mode." " will be displayed in debug mode
  6. Escape characters are not allowed


    Uncaught SyntaxError: Invalid or unexpected token." " will be displayed in debug mode
  7. Assignments to read-only properties are not allowed


    Uncaught TypeError: Cannot assign to read only property 'x' of object '#<Object>'." " will be displayed in debug mode
  8. Assignment to a property read using a getter method is not allowed


    Uncaught TypeError: Cannot set property x of #<Object> which has only a getter." " will be displayed in debug mode
  9. Not allowed to delete an attribute that is not allowed to be deleted


    Uncaught TypeError: Cannot delete property 'prototype' of function Object() { [native code] }." " will be displayed in debug mode
  10. Variable name cannot use "eval" string/"arguments" string

    Uncaught SyntaxError: Unexpected eval or arguments in strict mode." " will be displayed in debug mode
  11. Statements like the following are not allowed


    Uncaught SyntaxError: Strict mode code may not include a with statement." " will be displayed in debug mode
  12. For some security reasons, variables created in scope eval() cannot be called

    Uncaught ReferenceError: x is not defined." " will be displayed in debug mode

At the same time, in order to transition to new versions of Javascript in the future, strict mode adds some reserved keywords:

  • implements
  • interface
  • let
  • package
  • private
  • protected
  • public
  • static
  • yield

For example, if our variable name is one of the reserved keywords 'public', an error " Uncaught SyntaxError: Unexpected strict mode reserved word" will be reported.

Reference link

  1. http://www-lia.deis.unibo.it/materiale/JS/developer.mozilla.org/en-US/docs/Web/JavaScript/New_in_JavaScript/1.8-2.html
  2. https://developer.mozilla.org/en-US/docs/web/javascript/reference/strict_mode
  3. https://developer.mozilla.org/en-US/docs/Glossary/Sloppy_mode
  4. https://www.runoob.com/js/js-strict.html

Guess you like

Origin blog.csdn.net/devcloud/article/details/124091221