JavaScript Use Strict

JavaScript strict mode runs under strict conditions.

Use "use strict" directive

The "use strict" directive was added in JavaScript 1.8.5 (ECMAScript5).

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

The purpose of

"use strict" is to specify that code executes under strict conditions.

You cannot use undeclared variables in strict mode.

Browser that supports strict mode: Internet Explorer 10+, Firefox 4+ Chrome 13+, Safari 5.1+, Opera 12+.

Strict mode declaration

Strict mode is declared by adding a "use strict"; expression to the head of a script or function.

In the example, we can press F12 in the browser (or click "Tools >More tools>Developer Tools ") Turn on debug mode and view error messages.

Why use strict mode:

    Eliminate some unreasonable and rigorous Javascript syntax and reduce some weird behaviors;
  • Eliminate some insecure aspects of code running and ensure the safety of code running;
  • Improve compiler efficiency and increase running speed;
  • Pave the way for future versions of Javascript.

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

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 run in "strict mode". Mastering these contents will help you understand Javascript in more detail and make you a better programmer.

Reserved keywords

In order to transition to a new version of Javascript in the future, strict mode adds some reserved keywords:

  • implements
  • interface
  • let
  • package
  • private
  • protected
  • public
  • static
  • yield
"use strict";
var public = 1500;      //Error