SoFunction
Updated on 2025-03-09

Example analysis of JavaScript strict usage

This article describes the usage of JavaScript strict mode. Share it for your reference, as follows:

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

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

Undeclared variables are not allowed.

"use strict";
myFunction();
function myFunction() {
  y = 3.14;  // Error (y is not defined)}

Declaring internal to the function is a local scope (using strict patterns only within the function):

x = 3.14;    // No errors reportedmyFunction();
function myFunction() {
  "use strict";
  y = 3.14;  // Error (y is not defined)}

Not allowed to delete variables or objects

"use strict";
var x = 3.14;
delete x;        // Report an error

Function deletion is not allowed.

"use strict";
function x(p1, p2) {}; 
delete x;        // Report an error

Variable duplicate names are not allowed:

"use strict";
function x(p1, p1) {};  // Report an error

Octal is not allowed:

"use strict";
var x = 010;       // Report an error

Escape characters are not allowed:

"use strict";
var x = \010;      // Report an error

Assigning values ​​to read-only attributes is not allowed:

"use strict";
var obj = {};
(obj, "x", {value:0, writable:false});
 = 3.14;      // Report an error

Deletion of an attribute that is not allowed is not allowed:

"use strict";
delete ; // Report an error

The variable name cannot use the "eval" string:

"use strict";
var eval = 3.14;     // Report an error

The variable name cannot use the "arguments" string:

"use strict";
var arguments = 3.14;  // Report an error

Forbid this keyword to point to global objects

Why use strict mode:

  • Eliminate some unreasonable and imperfect aspects of Javascript syntax and reduce some weird behaviors;
  • Eliminate some insecurities in code operation and ensure the safety of code operation;
  • Improve compiler efficiency and increase running speed;
  • "Strict Mode" reflects the more reasonable, safer and more rigorous development direction of Javascript. Mainstream browsers including IE 10 have already supported it.

Interested friends can also use this siteOnline HTML/CSS/JavaScript code running toolhttp://tools./code/HtmlJsRunTest the above code running results.

For more information about JavaScript, you can also view the special topic of this site: "JavaScript object-oriented tutorial》、《Summary of JavaScript Errors and Debugging Skills》、《Summary of JavaScript data structure and algorithm techniques》、《JavaScript traversal algorithm and skills summary"and"Summary of JavaScript mathematical operations usage

I hope this article will be helpful to everyone's JavaScript programming.