JavaScript strict mode (use strict)

JavaScript strict mode (use strict)

JavaScript strict mode (strict mode) runs under strict conditions.

Use the "use strict" directive

The "use strict" directive is new in JavaScript 1.8.5 (ECMAScript5).

It is not a statement, but a literal expression, which will be ignored in older versions of JavaScript.

The purpose of "use strict" is to specify that the code is executed under strict conditions.

You cannot use undeclared variables in strict mode.

The main purposes of establishing "strict mode" are as follows:

- Eliminate some unreasonable and imprecise aspects of Javascript syntax and reduce some weird behaviors;

- Eliminate some insecurities in code running and ensure the safety of code running;

- Improve compiler efficiency and increase running speed;

- Pave the way for new versions of Javascript in the future .

1. Overview

In addition to the normal operating mode, ECMAscript 5 adds a second operating mode: "strict mode". As the name suggests, this mode makes Javascript run under stricter conditions.

2. Why use strict mode

- Eliminate some unreasonable and imprecise aspects of Javascript syntax and reduce some weird behaviors;

- Eliminate some inconveniences in code running A safe place to ensure the safety of code running;

- Improve compiler efficiency and increase running speed;

- Pave the way for new versions of Javascript in the future.

"Strict Mode" reflects the more reasonable, safer and more rigorous development direction of Javascript. Mainstream browsers, including IE 10, already support it, and many large projects have begun to fully embrace it.

On the other hand, the same code may have different running results in "strict mode"; some statements that can be run in "normal mode" will not be able to be run in "strict mode" run. Mastering these contents will help you understand Javascript in more detail and make you a better programmer.

3. Enter the flag

"use strict";

4.How to call

4.1For a single script

 

Continuing Learning
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> <body> <h1>使用 "use strict":</h1> <h3>严格模式不允许使用保留关键字。</h3> <p>浏览器按下 F12 开启调试模式,查看报错信息。</p> <script> "use strict"; var public = 1500; // 报错 </script> </body> </html>
submitReset Code